/vendor/symfony/symfony/src/Symfony/Component/ClassLoader/ApcClassLoader.php

https://github.com/nattaphat/hgis · PHP · 137 lines · 44 code · 15 blank · 78 comment · 4 complexity · 86d9a3b8315c0692848769a9c1c3e9c5 MD5 · raw file

  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. /**
  12. * ApcClassLoader implements a wrapping autoloader cached in APC for PHP 5.3.
  13. *
  14. * It expects an object implementing a findFile method to find the file. This
  15. * allow using it as a wrapper around the other loaders of the component (the
  16. * ClassLoader and the UniversalClassLoader for instance) but also around any
  17. * other autoloader following this convention (the Composer one for instance)
  18. *
  19. * $loader = new ClassLoader();
  20. *
  21. * // register classes with namespaces
  22. * $loader->add('Symfony\Component', __DIR__.'/component');
  23. * $loader->add('Symfony', __DIR__.'/framework');
  24. *
  25. * $cachedLoader = new ApcClassLoader('my_prefix', $loader);
  26. *
  27. * // activate the cached autoloader
  28. * $cachedLoader->register();
  29. *
  30. * // eventually deactivate the non-cached loader if it was registered previously
  31. * // to be sure to use the cached one.
  32. * $loader->unregister();
  33. *
  34. * @author Fabien Potencier <fabien@symfony.com>
  35. * @author Kris Wallsmith <kris@symfony.com>
  36. *
  37. * @api
  38. */
  39. class ApcClassLoader
  40. {
  41. private $prefix;
  42. /**
  43. * The class loader object being decorated.
  44. *
  45. * @var \Symfony\Component\ClassLoader\ClassLoader
  46. * A class loader object that implements the findFile() method.
  47. */
  48. protected $decorated;
  49. /**
  50. * Constructor.
  51. *
  52. * @param string $prefix The APC 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. * @api
  59. */
  60. public function __construct($prefix, $decorated)
  61. {
  62. if (!extension_loaded('apc')) {
  63. throw new \RuntimeException('Unable to use ApcClassLoader as APC is not enabled.');
  64. }
  65. if (!method_exists($decorated, 'findFile')) {
  66. throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
  67. }
  68. $this->prefix = $prefix;
  69. $this->decorated = $decorated;
  70. }
  71. /**
  72. * Registers this instance as an autoloader.
  73. *
  74. * @param Boolean $prepend Whether to prepend the autoloader or not
  75. */
  76. public function register($prepend = false)
  77. {
  78. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  79. }
  80. /**
  81. * Unregisters this instance as an autoloader.
  82. */
  83. public function unregister()
  84. {
  85. spl_autoload_unregister(array($this, 'loadClass'));
  86. }
  87. /**
  88. * Loads the given class or interface.
  89. *
  90. * @param string $class The name of the class
  91. *
  92. * @return Boolean|null True, if loaded
  93. */
  94. public function loadClass($class)
  95. {
  96. if ($file = $this->findFile($class)) {
  97. require $file;
  98. return true;
  99. }
  100. }
  101. /**
  102. * Finds a file by class name while caching lookups to APC.
  103. *
  104. * @param string $class A class name to resolve to file
  105. *
  106. * @return string|null
  107. */
  108. public function findFile($class)
  109. {
  110. if (false === $file = apc_fetch($this->prefix.$class)) {
  111. apc_store($this->prefix.$class, $file = $this->decorated->findFile($class));
  112. }
  113. return $file;
  114. }
  115. /**
  116. * Passes through all unknown calls onto the decorated object.
  117. */
  118. public function __call($method, $args)
  119. {
  120. return call_user_func_array(array($this->decorated, $method), $args);
  121. }
  122. }