XcacheClassLoader.php 3.9 KB

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