ApcClassLoader.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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__.'\ApcClassLoader 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. * ApcClassLoader implements a wrapping autoloader cached in APC 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. * use Symfony\Component\ClassLoader\ClassLoader;
  22. *
  23. * $loader = new ClassLoader();
  24. * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
  25. * $loader->addPrefix('Symfony', __DIR__.'/framework');
  26. *
  27. * // or with a Composer autoloader
  28. * use Composer\Autoload\ClassLoader;
  29. *
  30. * $loader = new ClassLoader();
  31. * $loader->add('Symfony\Component', __DIR__.'/component');
  32. * $loader->add('Symfony', __DIR__.'/framework');
  33. *
  34. * $cachedLoader = new ApcClassLoader('my_prefix', $loader);
  35. *
  36. * // activate the cached autoloader
  37. * $cachedLoader->register();
  38. *
  39. * // eventually deactivate the non-cached loader if it was registered previously
  40. * // to be sure to use the cached one.
  41. * $loader->unregister();
  42. *
  43. * @author Fabien Potencier <fabien@symfony.com>
  44. * @author Kris Wallsmith <kris@symfony.com>
  45. *
  46. * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead.
  47. */
  48. class ApcClassLoader
  49. {
  50. private $prefix;
  51. /**
  52. * A class loader object that implements the findFile() method.
  53. *
  54. * @var object
  55. */
  56. protected $decorated;
  57. /**
  58. * @param string $prefix The APC namespace prefix to use
  59. * @param object $decorated A class loader object that implements the findFile() method
  60. *
  61. * @throws \RuntimeException
  62. * @throws \InvalidArgumentException
  63. */
  64. public function __construct($prefix, $decorated)
  65. {
  66. if (!\function_exists('apcu_fetch')) {
  67. throw new \RuntimeException('Unable to use ApcClassLoader as APC is not installed.');
  68. }
  69. if (!method_exists($decorated, 'findFile')) {
  70. throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
  71. }
  72. $this->prefix = $prefix;
  73. $this->decorated = $decorated;
  74. }
  75. /**
  76. * Registers this instance as an autoloader.
  77. *
  78. * @param bool $prepend Whether to prepend the autoloader or not
  79. */
  80. public function register($prepend = false)
  81. {
  82. spl_autoload_register([$this, 'loadClass'], true, $prepend);
  83. }
  84. /**
  85. * Unregisters this instance as an autoloader.
  86. */
  87. public function unregister()
  88. {
  89. spl_autoload_unregister([$this, 'loadClass']);
  90. }
  91. /**
  92. * Loads the given class or interface.
  93. *
  94. * @param string $class The name of the class
  95. *
  96. * @return bool|null True, if loaded
  97. */
  98. public function loadClass($class)
  99. {
  100. if ($file = $this->findFile($class)) {
  101. require $file;
  102. return true;
  103. }
  104. return null;
  105. }
  106. /**
  107. * Finds a file by class name while caching lookups to APC.
  108. *
  109. * @param string $class A class name to resolve to file
  110. *
  111. * @return string|null
  112. */
  113. public function findFile($class)
  114. {
  115. $file = apcu_fetch($this->prefix.$class, $success);
  116. if (!$success) {
  117. apcu_store($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null);
  118. }
  119. return $file;
  120. }
  121. /**
  122. * Passes through all unknown calls onto the decorated object.
  123. */
  124. public function __call($method, $args)
  125. {
  126. return \call_user_func_array([$this->decorated, $method], $args);
  127. }
  128. }