Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Zend_Loader_Autoloader_Resource Class Reference
Inheritance diagram for Zend_Loader_Autoloader_Resource:
Zend_Loader_Autoloader_Interface

Public Member Functions

 __construct ($options)
 
 __call ($method, $args)
 
 getClassPath ($class)
 
 autoload ($class)
 
 setOptions (array $options)
 
 setNamespace ($namespace)
 
 getNamespace ()
 
 setBasePath ($path)
 
 getBasePath ()
 
 addResourceType ($type, $path, $namespace=null)
 
 addResourceTypes (array $types)
 
 setResourceTypes (array $types)
 
 getResourceTypes ()
 
 hasResourceType ($type)
 
 removeResourceType ($type)
 
 clearResourceTypes ()
 
 setDefaultResourceType ($type)
 
 getDefaultResourceType ()
 
 load ($resource, $type=null)
 

Protected Attributes

 $_basePath
 
 $_components = array()
 
 $_defaultResourceType
 
 $_namespace
 
 $_resourceTypes = array()
 

Detailed Description

Definition at line 35 of file Resource.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options)

Constructor

Parameters
array | Zend_Config$optionsConfiguration options for resource autoloader
Returns
void

Definition at line 68 of file Resource.php.

69  {
70  if ($options instanceof Zend_Config) {
71  $options = $options->toArray();
72  }
73  if (!is_array($options)) {
74  #require_once 'Zend/Loader/Exception.php';
75  throw new Zend_Loader_Exception('Options must be passed to resource loader constructor');
76  }
77 
78  $this->setOptions($options);
79 
80  $namespace = $this->getNamespace();
81  if ((null === $namespace)
82  || (null === $this->getBasePath())
83  ) {
84  #require_once 'Zend/Loader/Exception.php';
85  throw new Zend_Loader_Exception('Resource loader requires both a namespace and a base path for initialization');
86  }
87 
88  if (!empty($namespace)) {
89  $namespace .= '_';
90  }
91  #require_once 'Zend/Loader/Autoloader.php';
92  Zend_Loader_Autoloader::getInstance()->unshiftAutoloader($this, $namespace);
93  }

Member Function Documentation

◆ __call()

__call (   $method,
  $args 
)

Overloading: methods

Allow retrieving concrete resource object instances using 'get<Resourcename>()' syntax. Example: $loader = new Zend_Loader_Autoloader_Resource(array( 'namespace' => 'Stuff_', 'basePath' => '/path/to/some/stuff', )) $loader->addResourceType('Model', 'models', 'Model');

$foo = $loader->getModel('Foo'); // get instance of Stuff_Model_Foo class

Parameters
string$method
array$args
Returns
mixed
Exceptions
Zend_Loader_Exceptionif method not beginning with 'get' or not matching a valid resource type is called

Definition at line 115 of file Resource.php.

116  {
117  if ('get' == substr($method, 0, 3)) {
118  $type = strtolower(substr($method, 3));
119  if (!$this->hasResourceType($type)) {
120  #require_once 'Zend/Loader/Exception.php';
121  throw new Zend_Loader_Exception("Invalid resource type $type; cannot load resource");
122  }
123  if (empty($args)) {
124  #require_once 'Zend/Loader/Exception.php';
125  throw new Zend_Loader_Exception("Cannot load resources; no resource specified");
126  }
127  $resource = array_shift($args);
128  return $this->load($resource, $type);
129  }
130 
131  #require_once 'Zend/Loader/Exception.php';
132  throw new Zend_Loader_Exception("Method '$method' is not supported");
133  }
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$method
Definition: info.phtml:13
load($resource, $type=null)
Definition: Resource.php:452

◆ addResourceType()

addResourceType (   $type,
  $path,
  $namespace = null 
)

Add resource type

Parameters
string$typeidentifier for the resource type being loaded
string$pathpath relative to resource base path containing the resource types
null | string$namespacesub-component namespace to append to base namespace that qualifies this resource type
Returns
Zend_Loader_Autoloader_Resource

Definition at line 282 of file Resource.php.

283  {
284  $type = strtolower($type);
285  if (!isset($this->_resourceTypes[$type])) {
286  if (null === $namespace) {
287  #require_once 'Zend/Loader/Exception.php';
288  throw new Zend_Loader_Exception('Initial definition of a resource type must include a namespace');
289  }
290  $namespaceTopLevel = $this->getNamespace();
291  $namespace = ucfirst(trim($namespace, '_'));
292  $this->_resourceTypes[$type] = array(
293  'namespace' => empty($namespaceTopLevel) ? $namespace : $namespaceTopLevel . '_' . $namespace,
294  );
295  }
296  if (!is_string($path)) {
297  #require_once 'Zend/Loader/Exception.php';
298  throw new Zend_Loader_Exception('Invalid path specification provided; must be string');
299  }
300  $this->_resourceTypes[$type]['path'] = $this->getBasePath() . '/' . rtrim($path, '\/');
301 
302  $component = $this->_resourceTypes[$type]['namespace'];
303  $this->_components[$component] = $this->_resourceTypes[$type]['path'];
304  return $this;
305  }
$type
Definition: item.phtml:13

◆ addResourceTypes()

addResourceTypes ( array  $types)

Add multiple resources at once

$types should be an associative array of resource type => specification pairs. Each specification should be an associative array containing minimally the 'path' key (specifying the path relative to the resource base path) and optionally the 'namespace' key (indicating the subcomponent namespace to append to the resource namespace).

As an example: $loader->addResourceTypes(array( 'model' => array( 'path' => 'models', 'namespace' => 'Model', ), 'form' => array( 'path' => 'forms', 'namespace' => 'Form', ), ));

Parameters
array$types
Returns
Zend_Loader_Autoloader_Resource

Definition at line 333 of file Resource.php.

334  {
335  foreach ($types as $type => $spec) {
336  if (!is_array($spec)) {
337  #require_once 'Zend/Loader/Exception.php';
338  throw new Zend_Loader_Exception('addResourceTypes() expects an array of arrays');
339  }
340  if (!isset($spec['path'])) {
341  #require_once 'Zend/Loader/Exception.php';
342  throw new Zend_Loader_Exception('addResourceTypes() expects each array to include a paths element');
343  }
344  $paths = $spec['path'];
345  $namespace = null;
346  if (isset($spec['namespace'])) {
347  $namespace = $spec['namespace'];
348  }
349  $this->addResourceType($type, $paths, $namespace);
350  }
351  return $this;
352  }
$type
Definition: item.phtml:13
addResourceType($type, $path, $namespace=null)
Definition: Resource.php:282
$paths
Definition: _bootstrap.php:83

◆ autoload()

autoload (   $class)

Attempt to autoload a class

Parameters
string$class
Returns
mixed False if not matched, otherwise result if include operation

Implements Zend_Loader_Autoloader_Interface.

Definition at line 197 of file Resource.php.

198  {
199  $classPath = $this->getClassPath($class);
200  if (false !== $classPath) {
201  return include $classPath;
202  }
203  return false;
204  }
$_option $_optionId $class
Definition: date.phtml:13

◆ clearResourceTypes()

clearResourceTypes ( )

Clear all resource types

Returns
Zend_Loader_Autoloader_Resource

Definition at line 409 of file Resource.php.

410  {
411  $this->_resourceTypes = array();
412  $this->_components = array();
413  return $this;
414  }

◆ getBasePath()

getBasePath ( )

Get base path to this set of resources

Returns
string

Definition at line 269 of file Resource.php.

270  {
271  return $this->_basePath;
272  }

◆ getClassPath()

getClassPath (   $class)

Helper method to calculate the correct class path

Parameters
string$class
Returns
False if not matched other wise the correct path

Definition at line 141 of file Resource.php.

142  {
143  $segments = explode('_', $class);
144  $namespaceTopLevel = $this->getNamespace();
145  $namespace = '';
146 
147  if (!empty($namespaceTopLevel)) {
148  $namespace = array();
149  $topLevelSegments = count(explode('_', $namespaceTopLevel));
150  for ($i = 0; $i < $topLevelSegments; $i++) {
151  $namespace[] = array_shift($segments);
152  }
153  $namespace = implode('_', $namespace);
154  if ($namespace != $namespaceTopLevel) {
155  // wrong prefix? we're done
156  return false;
157  }
158  }
159 
160  if (count($segments) < 2) {
161  // assumes all resources have a component and class name, minimum
162  return false;
163  }
164 
165  $final = array_pop($segments);
166  $component = $namespace;
167  $lastMatch = false;
168  do {
169  $segment = array_shift($segments);
170  $component .= empty($component) ? $segment : '_' . $segment;
171  if (isset($this->_components[$component])) {
172  $lastMatch = $component;
173  }
174  } while (count($segments));
175 
176  if (!$lastMatch) {
177  return false;
178  }
179 
180  $final = substr($class, strlen($lastMatch) + 1);
181  $path = $this->_components[$lastMatch];
182  $classPath = $path . '/' . str_replace('_', '/', $final) . '.php';
183 
184  if (Zend_Loader::isReadable($classPath)) {
185  return $classPath;
186  }
187 
188  return false;
189  }
static isReadable($filename)
Definition: Loader.php:162
$_option $_optionId $class
Definition: date.phtml:13
$i
Definition: gallery.phtml:31

◆ getDefaultResourceType()

getDefaultResourceType ( )

Get default resource type to use when calling load()

Returns
string|null

Definition at line 435 of file Resource.php.

436  {
438  }

◆ getNamespace()

getNamespace ( )

Get namespace this autoloader handles

Returns
string

Definition at line 247 of file Resource.php.

248  {
249  return $this->_namespace;
250  }

◆ getResourceTypes()

getResourceTypes ( )

Retrieve resource type mappings

Returns
array

Definition at line 372 of file Resource.php.

373  {
374  return $this->_resourceTypes;
375  }

◆ hasResourceType()

hasResourceType (   $type)

Is the requested resource type defined?

Parameters
string$type
Returns
bool

Definition at line 383 of file Resource.php.

384  {
385  return isset($this->_resourceTypes[$type]);
386  }
$type
Definition: item.phtml:13

◆ load()

load (   $resource,
  $type = null 
)

Object registry and factory

Loads the requested resource of type $type (or uses the default resource type if none provided). If the resource has been loaded previously, returns the previous instance; otherwise, instantiates it.

Parameters
string$resource
string$type
Returns
object
Exceptions
Zend_Loader_Exceptionif resource type not specified or invalid

Definition at line 452 of file Resource.php.

453  {
454  if (null === $type) {
455  $type = $this->getDefaultResourceType();
456  if (empty($type)) {
457  #require_once 'Zend/Loader/Exception.php';
458  throw new Zend_Loader_Exception('No resource type specified');
459  }
460  }
461  if (!$this->hasResourceType($type)) {
462  #require_once 'Zend/Loader/Exception.php';
463  throw new Zend_Loader_Exception('Invalid resource type specified');
464  }
465  $namespace = $this->_resourceTypes[$type]['namespace'];
466  $class = $namespace . '_' . ucfirst($resource);
467  if (!isset($this->_resources[$class])) {
468  $this->_resources[$class] = new $class;
469  }
470  return $this->_resources[$class];
471  }
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13

◆ removeResourceType()

removeResourceType (   $type)

Remove the requested resource type

Parameters
string$type
Returns
Zend_Loader_Autoloader_Resource

Definition at line 394 of file Resource.php.

395  {
396  if ($this->hasResourceType($type)) {
397  $namespace = $this->_resourceTypes[$type]['namespace'];
398  unset($this->_components[$namespace]);
399  unset($this->_resourceTypes[$type]);
400  }
401  return $this;
402  }
$type
Definition: item.phtml:13

◆ setBasePath()

setBasePath (   $path)

Set base path for this set of resources

Parameters
string$path
Returns
Zend_Loader_Autoloader_Resource

Definition at line 258 of file Resource.php.

259  {
260  $this->_basePath = (string) $path;
261  return $this;
262  }

◆ setDefaultResourceType()

setDefaultResourceType (   $type)

Set default resource type to use when calling load()

Parameters
string$type
Returns
Zend_Loader_Autoloader_Resource

Definition at line 422 of file Resource.php.

423  {
424  if ($this->hasResourceType($type)) {
425  $this->_defaultResourceType = $type;
426  }
427  return $this;
428  }
$type
Definition: item.phtml:13

◆ setNamespace()

setNamespace (   $namespace)

Set namespace that this autoloader handles

Parameters
string$namespace
Returns
Zend_Loader_Autoloader_Resource

Definition at line 236 of file Resource.php.

237  {
238  $this->_namespace = rtrim((string) $namespace, '_');
239  return $this;
240  }

◆ setOptions()

setOptions ( array  $options)

Set class state from options

Parameters
array$options
Returns
Zend_Loader_Autoloader_Resource

Definition at line 212 of file Resource.php.

213  {
214  // Set namespace first, see ZF-10836
215  if (isset($options['namespace'])) {
216  $this->setNamespace($options['namespace']);
217  unset($options['namespace']);
218  }
219 
220  $methods = get_class_methods($this);
221  foreach ($options as $key => $value) {
222  $method = 'set' . ucfirst($key);
223  if (in_array($method, $methods)) {
224  $this->$method($value);
225  }
226  }
227  return $this;
228  }
$methods
Definition: billing.phtml:71
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13

◆ setResourceTypes()

setResourceTypes ( array  $types)

Overwrite existing and set multiple resource types at once

See also
Zend_Loader_Autoloader_Resource::addResourceTypes()
Parameters
array$types
Returns
Zend_Loader_Autoloader_Resource

Definition at line 361 of file Resource.php.

362  {
363  $this->clearResourceTypes();
364  return $this->addResourceTypes($types);
365  }

Field Documentation

◆ $_basePath

$_basePath
protected

Definition at line 40 of file Resource.php.

◆ $_components

$_components = array()
protected

Definition at line 45 of file Resource.php.

◆ $_defaultResourceType

$_defaultResourceType
protected

Definition at line 50 of file Resource.php.

◆ $_namespace

$_namespace
protected

Definition at line 55 of file Resource.php.

◆ $_resourceTypes

$_resourceTypes = array()
protected

Definition at line 60 of file Resource.php.


The documentation for this class was generated from the following file: