Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Resource.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Loader/Autoloader/Interface.php';
25 
36 {
40  protected $_basePath;
41 
45  protected $_components = array();
46 
51 
55  protected $_namespace;
56 
60  protected $_resourceTypes = array();
61 
68  public function __construct($options)
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  }
94 
115  public function __call($method, $args)
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  }
134 
141  public function getClassPath($class)
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  }
190 
197  public function autoload($class)
198  {
199  $classPath = $this->getClassPath($class);
200  if (false !== $classPath) {
201  return include $classPath;
202  }
203  return false;
204  }
205 
212  public function setOptions(array $options)
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  }
229 
236  public function setNamespace($namespace)
237  {
238  $this->_namespace = rtrim((string) $namespace, '_');
239  return $this;
240  }
241 
247  public function getNamespace()
248  {
249  return $this->_namespace;
250  }
251 
258  public function setBasePath($path)
259  {
260  $this->_basePath = (string) $path;
261  return $this;
262  }
263 
269  public function getBasePath()
270  {
271  return $this->_basePath;
272  }
273 
282  public function addResourceType($type, $path, $namespace = null)
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  }
306 
333  public function addResourceTypes(array $types)
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  }
353 
361  public function setResourceTypes(array $types)
362  {
363  $this->clearResourceTypes();
364  return $this->addResourceTypes($types);
365  }
366 
372  public function getResourceTypes()
373  {
374  return $this->_resourceTypes;
375  }
376 
383  public function hasResourceType($type)
384  {
385  return isset($this->_resourceTypes[$type]);
386  }
387 
394  public function removeResourceType($type)
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  }
403 
409  public function clearResourceTypes()
410  {
411  $this->_resourceTypes = array();
412  $this->_components = array();
413  return $this;
414  }
415 
422  public function setDefaultResourceType($type)
423  {
424  if ($this->hasResourceType($type)) {
425  $this->_defaultResourceType = $type;
426  }
427  return $this;
428  }
429 
435  public function getDefaultResourceType()
436  {
438  }
439 
452  public function load($resource, $type = null)
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  }
472 }
$resource
Definition: bulk.php:12
static isReadable($filename)
Definition: Loader.php:162
$type
Definition: item.phtml:13
$methods
Definition: billing.phtml:71
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
load($resource, $type=null)
Definition: Resource.php:452
addResourceType($type, $path, $namespace=null)
Definition: Resource.php:282
$paths
Definition: _bootstrap.php:83
$i
Definition: gallery.phtml:31