Drupal investigation

WinCacheClassLoader.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * WinCacheClassLoader implements a wrapping autoloader cached in WinCache.
  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 autoloaders following this convention (the Composer one for instance).
  18. *
  19. * // with a Symfony autoloader
  20. * use Symfony\Component\ClassLoader\ClassLoader;
  21. *
  22. * $loader = new ClassLoader();
  23. * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
  24. * $loader->addPrefix('Symfony', __DIR__.'/framework');
  25. *
  26. * // or with a Composer autoloader
  27. * use Composer\Autoload\ClassLoader;
  28. *
  29. * $loader = new ClassLoader();
  30. * $loader->add('Symfony\Component', __DIR__.'/component');
  31. * $loader->add('Symfony', __DIR__.'/framework');
  32. *
  33. * $cachedLoader = new WinCacheClassLoader('my_prefix', $loader);
  34. *
  35. * // activate the cached autoloader
  36. * $cachedLoader->register();
  37. *
  38. * // eventually deactivate the non-cached loader if it was registered previously
  39. * // to be sure to use the cached one.
  40. * $loader->unregister();
  41. *
  42. * @author Fabien Potencier <fabien@symfony.com>
  43. * @author Kris Wallsmith <kris@symfony.com>
  44. * @author Artem Ryzhkov <artem@smart-core.org>
  45. */
  46. class WinCacheClassLoader
  47. {
  48. private $prefix;
  49. /**
  50. * A class loader object that implements the findFile() method.
  51. *
  52. * @var object
  53. */
  54. protected $decorated;
  55. /**
  56. * Constructor.
  57. *
  58. * @param string $prefix The WinCache 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 (!extension_loaded('wincache')) {
  67. throw new \RuntimeException('Unable to use WinCacheClassLoader as WinCache is not enabled.');
  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(array($this, 'loadClass'), true, $prepend);
  83. }
  84. /**
  85. * Unregisters this instance as an autoloader.
  86. */
  87. public function unregister()
  88. {
  89. spl_autoload_unregister(array($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. }
  105. /**
  106. * Finds a file by class name while caching lookups to WinCache.
  107. *
  108. * @param string $class A class name to resolve to file
  109. *
  110. * @return string|null
  111. */
  112. public function findFile($class)
  113. {
  114. $file = wincache_ucache_get($this->prefix.$class, $success);
  115. if (!$success) {
  116. wincache_ucache_set($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null, 0);
  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. }