PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lluccio-panellcontrol/vendor/symfony/symfony/src/Symfony/Component/Config/Resource/ReflectionClassResource.php

https://bitbucket.org/javiausias/m12-lluccio
PHP | 200 lines | 151 code | 30 blank | 19 comment | 23 complexity | 4cc2fbacb8901dad4c9312037ca39c1e MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Unlicense, BSD-3-Clause
  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\Config\Resource;
  11. use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class ReflectionClassResource implements SelfCheckingResourceInterface, \Serializable
  17. {
  18. private $files = array();
  19. private $className;
  20. private $classReflector;
  21. private $excludedVendors = array();
  22. private $hash;
  23. public function __construct(\ReflectionClass $classReflector, $excludedVendors = array())
  24. {
  25. $this->className = $classReflector->name;
  26. $this->classReflector = $classReflector;
  27. $this->excludedVendors = $excludedVendors;
  28. }
  29. public function isFresh($timestamp)
  30. {
  31. if (null === $this->hash) {
  32. $this->hash = $this->computeHash();
  33. $this->loadFiles($this->classReflector);
  34. }
  35. foreach ($this->files as $file => $v) {
  36. if (false === $filemtime = @filemtime($file)) {
  37. return false;
  38. }
  39. if ($filemtime > $timestamp) {
  40. return $this->hash === $this->computeHash();
  41. }
  42. }
  43. return true;
  44. }
  45. public function __toString()
  46. {
  47. return 'reflection.'.$this->className;
  48. }
  49. public function serialize()
  50. {
  51. if (null === $this->hash) {
  52. $this->hash = $this->computeHash();
  53. $this->loadFiles($this->classReflector);
  54. }
  55. return serialize(array($this->files, $this->className, $this->hash));
  56. }
  57. public function unserialize($serialized)
  58. {
  59. list($this->files, $this->className, $this->hash) = unserialize($serialized);
  60. }
  61. private function loadFiles(\ReflectionClass $class)
  62. {
  63. foreach ($class->getInterfaces() as $v) {
  64. $this->loadFiles($v);
  65. }
  66. do {
  67. $file = $class->getFileName();
  68. if (false !== $file && file_exists($file)) {
  69. foreach ($this->excludedVendors as $vendor) {
  70. if (0 === strpos($file, $vendor) && false !== strpbrk(substr($file, strlen($vendor), 1), '/'.DIRECTORY_SEPARATOR)) {
  71. $file = false;
  72. break;
  73. }
  74. }
  75. if ($file) {
  76. $this->files[$file] = null;
  77. }
  78. }
  79. foreach ($class->getTraits() as $v) {
  80. $this->loadFiles($v);
  81. }
  82. } while ($class = $class->getParentClass());
  83. }
  84. private function computeHash()
  85. {
  86. if (null === $this->classReflector) {
  87. try {
  88. $this->classReflector = new \ReflectionClass($this->className);
  89. } catch (\ReflectionException $e) {
  90. // the class does not exist anymore
  91. return false;
  92. }
  93. }
  94. $hash = hash_init('md5');
  95. foreach ($this->generateSignature($this->classReflector) as $info) {
  96. hash_update($hash, $info);
  97. }
  98. return hash_final($hash);
  99. }
  100. private function generateSignature(\ReflectionClass $class)
  101. {
  102. yield $class->getDocComment();
  103. yield (int) $class->isFinal();
  104. yield (int) $class->isAbstract();
  105. if ($class->isTrait()) {
  106. yield print_r(class_uses($class->name), true);
  107. } else {
  108. yield print_r(class_parents($class->name), true);
  109. yield print_r(class_implements($class->name), true);
  110. yield print_r($class->getConstants(), true);
  111. }
  112. if (!$class->isInterface()) {
  113. $defaults = $class->getDefaultProperties();
  114. foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $p) {
  115. yield $p->getDocComment().$p;
  116. yield print_r($defaults[$p->name], true);
  117. }
  118. }
  119. if (defined('HHVM_VERSION')) {
  120. foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
  121. // workaround HHVM bug with variadics, see https://github.com/facebook/hhvm/issues/5762
  122. yield preg_replace('/^ @@.*/m', '', new ReflectionMethodHhvmWrapper($m->class, $m->name));
  123. }
  124. } else {
  125. foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
  126. yield preg_replace('/^ @@.*/m', '', $m);
  127. $defaults = array();
  128. foreach ($m->getParameters() as $p) {
  129. $defaults[$p->name] = $p->isDefaultValueAvailable() ? $p->getDefaultValue() : null;
  130. }
  131. yield print_r($defaults, true);
  132. }
  133. }
  134. if ($class->isAbstract() || $class->isInterface() || $class->isTrait()) {
  135. return;
  136. }
  137. if (interface_exists(EventSubscriberInterface::class, false) && $class->isSubclassOf(EventSubscriberInterface::class)) {
  138. yield EventSubscriberInterface::class;
  139. yield print_r(\call_user_func(array($class->name, 'getSubscribedEvents')), true);
  140. }
  141. if (interface_exists(ServiceSubscriberInterface::class, false) && $class->isSubclassOf(ServiceSubscriberInterface::class)) {
  142. yield ServiceSubscriberInterface::class;
  143. yield print_r(\call_user_func(array($class->name, 'getSubscribedServices')), true);
  144. }
  145. }
  146. }
  147. /**
  148. * @internal
  149. */
  150. class ReflectionMethodHhvmWrapper extends \ReflectionMethod
  151. {
  152. public function getParameters()
  153. {
  154. $params = array();
  155. foreach (parent::getParameters() as $i => $p) {
  156. $params[] = new ReflectionParameterHhvmWrapper(array($this->class, $this->name), $i);
  157. }
  158. return $params;
  159. }
  160. }
  161. /**
  162. * @internal
  163. */
  164. class ReflectionParameterHhvmWrapper extends \ReflectionParameter
  165. {
  166. public function getDefaultValue()
  167. {
  168. return array($this->isVariadic(), $this->isDefaultValueAvailable() ? parent::getDefaultValue() : null);
  169. }
  170. }