You are here

public function AuthcacheP13nObjectFactory::get in Authenticated User Page Caching (Authcache) 7.2

Return the value or instance for the given resource.

1 call to AuthcacheP13nObjectFactory::get()
AuthcacheP13nObjectFactory::resolveReferences in modules/authcache_p13n/includes/AuthcacheP13nObjectFactory.inc
Substitute resource references with their actual values.

File

modules/authcache_p13n/includes/AuthcacheP13nObjectFactory.inc, line 132
Defines the class AuthcacheP13nObjectFactory.

Class

AuthcacheP13nObjectFactory
A utility class helping with dependency injection.

Code

public function get($name) {
  $result = FALSE;
  if (isset($this->resources[$name])) {
    $r = $this->resources[$name];
    switch ($r['#type']) {
      case 'resolved':
        $result = $r['#value'];
        break;
      case 'value':
        $result = $this
          ->resolveReferences($r['#value']);
        break;
      case 'func':
        $arguments = !empty($r['#arguments']) ? $r['#arguments'] : array();
        $arguments = $this
          ->resolveReferences($arguments);
        $result = call_user_func_array($r['#func'], $arguments);
        break;
      case 'class':
        $arguments = !empty($r['#arguments']) ? $r['#arguments'] : array();
        $arguments = $this
          ->resolveReferences($arguments);
        try {
          $reflection = new ReflectionClass($r['#class']);

          // Work around https://bugs.php.net/bug.php?id=52854
          if (empty($arguments)) {
            $result = $reflection
              ->newInstance();
          }
          else {
            $result = $reflection
              ->newInstanceArgs($arguments);
          }
        } catch (Exception $e) {
          throw new AuthcacheP13nObjectFactoryException(format_string('Failed to create instance of class @class', array(
            '@class' => $r['#class'],
          )), 0, $e);
        }
        break;
      case 'collection':
        $result = array();
        $members = $this
          ->collectMembers($r['#collection']);
        $processor = isset($r['#processor']) ? $r['#processor'] : '';
        $processors = !empty($r['#processors']) ? $r['#processors'] : array();
        foreach ($members as $name => $definition) {
          $key = isset($definition['#key']) ? $definition['#key'] : $name;
          $ref = '@' . $name;
          if (isset($processors[$key])) {
            $ref .= '[' . $processors[$key] . ']';
          }
          elseif ($processor) {
            $ref .= '[' . $processor . ']';
          }
          $result[$key] = $this
            ->resolveReferences($ref);
        }
        break;
      default:
        throw new AuthcacheP13nObjectFactoryException('Unknown resource type: ' . $r['#type']);
    }
    $this->resources[$name] = array(
      '#type' => 'resolved',
      '#value' => $result,
    );
  }
  else {
    throw new AuthcacheP13nObjectFactoryException('No such resource: ' . $name);
  }
  return $result;
}