Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PluginLoader.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Loader/PluginLoader/Interface.php';
25 
27 #require_once 'Zend/Loader.php';
28 
38 class Zend_Loader_PluginLoader implements Zend_Loader_PluginLoader_Interface
39 {
44  protected static $_includeFileCache;
45 
50  protected static $_includeFileCacheHandler;
51 
57  protected $_loadedPluginPaths = array();
58 
64  protected $_loadedPlugins = array();
65 
71  protected $_prefixToPaths = array();
72 
78  protected static $_staticLoadedPluginPaths = array();
79 
85  protected static $_staticLoadedPlugins = array();
86 
92  protected static $_staticPrefixToPaths = array();
93 
99  protected $_useStaticRegistry = null;
100 
107  public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
108  {
109  if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
110  $this->_useStaticRegistry = $staticRegistryName;
111  if(!isset(self::$_staticPrefixToPaths[$staticRegistryName])) {
112  self::$_staticPrefixToPaths[$staticRegistryName] = array();
113  }
114  if(!isset(self::$_staticLoadedPlugins[$staticRegistryName])) {
115  self::$_staticLoadedPlugins[$staticRegistryName] = array();
116  }
117  }
118 
119  foreach ($prefixToPaths as $prefix => $path) {
120  $this->addPrefixPath($prefix, $path);
121  }
122  }
123 
130  protected function _formatPrefix($prefix)
131  {
132  if($prefix == "") {
133  return $prefix;
134  }
135 
136  $nsSeparator = (false !== strpos($prefix, '\\'))?'\\':'_';
137  $prefix = rtrim($prefix, $nsSeparator) . $nsSeparator;
138  //if $nsSeprator == "\" and the prefix ends in "_\" remove trailing \
139  //https://github.com/zendframework/zf1/issues/152
140  if(($nsSeparator == "\\") && (substr($prefix,-2) == "_\\")) {
141  $prefix = substr($prefix, 0, -1);
142  }
143  return $prefix;
144  }
145 
153  public function addPrefixPath($prefix, $path)
154  {
155  if (!is_string($prefix) || !is_string($path)) {
156  #require_once 'Zend/Loader/PluginLoader/Exception.php';
157  throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
158  }
159 
160  $prefix = $this->_formatPrefix($prefix);
161  $path = rtrim($path, '/\\') . '/';
162 
163  if ($this->_useStaticRegistry) {
164  self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
165  } else {
166  if (!isset($this->_prefixToPaths[$prefix])) {
167  $this->_prefixToPaths[$prefix] = array();
168  }
169  if (!in_array($path, $this->_prefixToPaths[$prefix])) {
170  $this->_prefixToPaths[$prefix][] = $path;
171  }
172  }
173  return $this;
174  }
175 
182  public function getPaths($prefix = null)
183  {
184  if ((null !== $prefix) && is_string($prefix)) {
185  $prefix = $this->_formatPrefix($prefix);
186  if ($this->_useStaticRegistry) {
187  if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
188  return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
189  }
190 
191  return false;
192  }
193 
194  if (isset($this->_prefixToPaths[$prefix])) {
195  return $this->_prefixToPaths[$prefix];
196  }
197 
198  return false;
199  }
200 
201  if ($this->_useStaticRegistry) {
202  return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
203  }
204 
205  return $this->_prefixToPaths;
206  }
207 
214  public function clearPaths($prefix = null)
215  {
216  if ((null !== $prefix) && is_string($prefix)) {
217  $prefix = $this->_formatPrefix($prefix);
218  if ($this->_useStaticRegistry) {
219  if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
220  unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
221  return true;
222  }
223 
224  return false;
225  }
226 
227  if (isset($this->_prefixToPaths[$prefix])) {
228  unset($this->_prefixToPaths[$prefix]);
229  return true;
230  }
231 
232  return false;
233  }
234 
235  if ($this->_useStaticRegistry) {
236  self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
237  } else {
238  $this->_prefixToPaths = array();
239  }
240 
241  return true;
242  }
243 
251  public function removePrefixPath($prefix, $path = null)
252  {
253  $prefix = $this->_formatPrefix($prefix);
254  if ($this->_useStaticRegistry) {
255  $registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
256  } else {
257  $registry =& $this->_prefixToPaths;
258  }
259 
260  if (!isset($registry[$prefix])) {
261  #require_once 'Zend/Loader/PluginLoader/Exception.php';
262  throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
263  }
264 
265  if ($path != null) {
266  $pos = array_search($path, $registry[$prefix]);
267  if (false === $pos) {
268  #require_once 'Zend/Loader/PluginLoader/Exception.php';
269  throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
270  }
271  unset($registry[$prefix][$pos]);
272  } else {
273  unset($registry[$prefix]);
274  }
275 
276  return $this;
277  }
278 
285  protected function _formatName($name)
286  {
287  return ucfirst((string) $name);
288  }
289 
296  public function isLoaded($name)
297  {
298  $name = $this->_formatName($name);
299  if ($this->_useStaticRegistry) {
300  return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
301  }
302 
303  return isset($this->_loadedPlugins[$name]);
304  }
305 
312  public function getClassName($name)
313  {
314  $name = $this->_formatName($name);
315  if ($this->_useStaticRegistry
316  && isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name])
317  ) {
318  return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
319  } elseif (isset($this->_loadedPlugins[$name])) {
320  return $this->_loadedPlugins[$name];
321  }
322 
323  return false;
324  }
325 
332  public function getClassPath($name)
333  {
334  $name = $this->_formatName($name);
335  if ($this->_useStaticRegistry
336  && !empty(self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name])
337  ) {
338  return self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name];
339  } elseif (!empty($this->_loadedPluginPaths[$name])) {
340  return $this->_loadedPluginPaths[$name];
341  }
342 
343  if ($this->isLoaded($name)) {
344  $class = $this->getClassName($name);
345  $r = new ReflectionClass($class);
346  $path = $r->getFileName();
347  if ($this->_useStaticRegistry) {
348  self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = $path;
349  } else {
350  $this->_loadedPluginPaths[$name] = $path;
351  }
352  return $path;
353  }
354 
355  return false;
356  }
357 
368  public function load($name, $throwExceptions = true)
369  {
370  $name = $this->_formatName($name);
371  if ($this->isLoaded($name)) {
372  return $this->getClassName($name);
373  }
374 
375  if ($this->_useStaticRegistry) {
376  $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
377  } else {
378  $registry = $this->_prefixToPaths;
379  }
380 
381  $registry = array_reverse($registry, true);
382  $found = false;
383  if (false !== strpos($name, '\\')) {
384  $classFile = str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php';
385  } else {
386  $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
387  }
388  $incFile = self::getIncludeFileCache();
389  foreach ($registry as $prefix => $paths) {
391 
392  if (class_exists($className, false)) {
393  $found = true;
394  break;
395  }
396 
397  $paths = array_reverse($paths, true);
398 
399  foreach ($paths as $path) {
400  $loadFile = $path . $classFile;
401  if (Zend_Loader::isReadable($loadFile)) {
402  include_once $loadFile;
403  if (class_exists($className, false)) {
404  if (null !== $incFile) {
405  self::_appendIncFile($loadFile);
406  }
407  $found = true;
408  break 2;
409  }
410  }
411  }
412  }
413 
414  if (!$found) {
415  if (!$throwExceptions) {
416  return false;
417  }
418 
419  $message = "Plugin by name '$name' was not found in the registry; used paths:";
420  foreach ($registry as $prefix => $paths) {
421  $message .= "\n$prefix: " . implode(PATH_SEPARATOR, $paths);
422  }
423  #require_once 'Zend/Loader/PluginLoader/Exception.php';
425  }
426 
427  if ($this->_useStaticRegistry) {
428  self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
429  } else {
430  $this->_loadedPlugins[$name] = $className;
431  }
432  return $className;
433  }
434 
445  public static function setIncludeFileCache($file)
446  {
447  if (!empty(self::$_includeFileCacheHandler)) {
448  flock(self::$_includeFileCacheHandler, LOCK_UN);
449  fclose(self::$_includeFileCacheHandler);
450  }
451 
452  self::$_includeFileCacheHandler = null;
453 
454  if (null === $file) {
455  self::$_includeFileCache = null;
456  return;
457  }
458 
459  if (!file_exists($file) && !file_exists(dirname($file))) {
460  #require_once 'Zend/Loader/PluginLoader/Exception.php';
461  throw new Zend_Loader_PluginLoader_Exception('Specified file does not exist and/or directory does not exist (' . $file . ')');
462  }
463  if (file_exists($file) && !is_writable($file)) {
464  #require_once 'Zend/Loader/PluginLoader/Exception.php';
465  throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
466  }
467  if (!file_exists($file) && file_exists(dirname($file)) && !is_writable(dirname($file))) {
468  #require_once 'Zend/Loader/PluginLoader/Exception.php';
469  throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
470  }
471 
472  self::$_includeFileCache = $file;
473  }
474 
480  public static function getIncludeFileCache()
481  {
482  return self::$_includeFileCache;
483  }
484 
491  protected static function _appendIncFile($incFile)
492  {
493  if (!isset(self::$_includeFileCacheHandler)) {
494  self::$_includeFileCacheHandler = fopen(self::$_includeFileCache, 'ab');
495 
496  if (!flock(self::$_includeFileCacheHandler, LOCK_EX | LOCK_NB, $wouldBlock) || $wouldBlock) {
497  self::$_includeFileCacheHandler = false;
498  }
499  }
500 
501  if (false !== self::$_includeFileCacheHandler) {
502  $line = "<?php include_once '$incFile'?>\n";
503  fwrite(self::$_includeFileCacheHandler, $line, strlen($line));
504  }
505  }
506 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$message
static isReadable($filename)
Definition: Loader.php:162
$prefix
Definition: name.phtml:25
$_option $_optionId $class
Definition: date.phtml:13
$pos
Definition: list.phtml:42
is_writable($path)
Definition: io.php:25
removePrefixPath($prefix, $path=null)
$paths
Definition: _bootstrap.php:83
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14