XcacheClassLoader.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader;
  11. @trigger_error('The '.__NAMESPACE__.'\XcacheClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use `composer install --apcu-autoloader` instead.', \E_USER_DEPRECATED);
  12. /**
  13. * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3.
  14. *
  15. * It expects an object implementing a findFile method to find the file. This
  16. * allows using it as a wrapper around the other loaders of the component (the
  17. * ClassLoader for instance) but also around any other autoloaders following
  18. * this convention (the Composer one for instance).
  19. *
  20. * // with a Symfony autoloader
  21. * $loader = new ClassLoader();
  22. * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
  23. * $loader->addPrefix('Symfony', __DIR__.'/framework');
  24. *
  25. * // or with a Composer autoloader
  26. * use Composer\Autoload\ClassLoader;
  27. *
  28. * $loader = new ClassLoader();
  29. * $loader->add('Symfony\Component', __DIR__.'/component');
  30. * $loader->add('Symfony', __DIR__.'/framework');
  31. *
  32. * $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
  33. *
  34. * // activate the cached autoloader
  35. * $cachedLoader->register();
  36. *
  37. * // eventually deactivate the non-cached loader if it was registered previously
  38. * // to be sure to use the cached one.
  39. * $loader->unregister();
  40. *
  41. * @author Fabien Potencier <fabien@symfony.com>
  42. * @author Kris Wallsmith <kris@symfony.com>
  43. * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
  44. *
  45. * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead.
  46. */
  47. class XcacheClassLoader
  48. {
  49. private $prefix;
  50. private $decorated;
  51. /**
  52. * @param string $prefix The XCache namespace prefix to use
  53. * @param object $decorated A class loader object that implements the findFile() method
  54. *
  55. * @throws \RuntimeException
  56. * @throws \InvalidArgumentException
  57. */
  58. public function __construct($prefix, $decorated)
  59. {
  60. if (!\extension_loaded('xcache')) {
  61. throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
  62. }
  63. if (!method_exists($decorated, 'findFile')) {
  64. throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
  65. }
  66. $this->prefix = $prefix;
  67. $this->decorated = $decorated;
  68. }
  69. /**
  70. * Registers this instance as an autoloader.
  71. *
  72. * @param bool $prepend Whether to prepend the autoloader or not
  73. */
  74. public function register($prepend = false)
  75. {
  76. spl_autoload_register([$this, 'loadClass'], true, $prepend);
  77. }
  78. /**
  79. * Unregisters this instance as an autoloader.
  80. */
  81. public function unregister()
  82. {
  83. spl_autoload_unregister([$this, 'loadClass']);
  84. }
  85. /**
  86. * Loads the given class or interface.
  87. *
  88. * @param string $class The name of the class
  89. *
  90. * @return bool|null True, if loaded
  91. */
  92. public function loadClass($class)
  93. {
  94. if ($file = $this->findFile($class)) {
  95. require $file;
  96. return true;
  97. }
  98. return null;
  99. }
  100. /**
  101. * Finds a file by class name while caching lookups to Xcache.
  102. *
  103. * @param string $class A class name to resolve to file
  104. *
  105. * @return string|null
  106. */
  107. public function findFile($class)
  108. {
  109. if (xcache_isset($this->prefix.$class)) {
  110. $file = xcache_get($this->prefix.$class);
  111. } else {
  112. $file = $this->decorated->findFile($class) ?: null;
  113. xcache_set($this->prefix.$class, $file);
  114. }
  115. return $file;
  116. }
  117. /**
  118. * Passes through all unknown calls onto the decorated object.
  119. */
  120. public function __call($method, $args)
  121. {
  122. return \call_user_func_array([$this->decorated, $method], $args);
  123. }
  124. }