|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Config\Resource; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Nicolas Grekas <p@tchwork.com> |
| 16 | + */ |
| 17 | +class ReflectionClassResource implements SelfCheckingResourceInterface, \Serializable |
| 18 | +{ |
| 19 | + private $file; |
| 20 | + private $className; |
| 21 | + private $classReflector; |
| 22 | + private $hash; |
| 23 | + |
| 24 | + public function __construct(\ReflectionClass $classReflector) |
| 25 | + { |
| 26 | + $this->file = $classReflector->getFileName(); |
| 27 | + if ($this->file && !file_exists($this->file)) { |
| 28 | + $this->file = false; |
| 29 | + } |
| 30 | + $this->className = $classReflector->name; |
| 31 | + $this->classReflector = $classReflector; |
| 32 | + $this->hash = $this->computeHash(); |
| 33 | + } |
| 34 | + |
| 35 | + public function isFresh($timestamp) |
| 36 | + { |
| 37 | + if ($this->file) { |
| 38 | + if (!file_exists($this->file)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + if (@filemtime($this->file) <= $timestamp) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return $this->hash === $this->computeHash(); |
| 48 | + } |
| 49 | + |
| 50 | + public function __toString() |
| 51 | + { |
| 52 | + return 'reflection.'.$this->className; |
| 53 | + } |
| 54 | + |
| 55 | + public function serialize() |
| 56 | + { |
| 57 | + return serialize(array($this->file, $this->className, $this->hash)); |
| 58 | + } |
| 59 | + |
| 60 | + public function unserialize($serialized) |
| 61 | + { |
| 62 | + list($this->file, $this->className, $this->hash) = unserialize($serialized); |
| 63 | + } |
| 64 | + |
| 65 | + private function computeHash() |
| 66 | + { |
| 67 | + if (null === $this->classReflector) { |
| 68 | + try { |
| 69 | + $this->classReflector = new \ReflectionClass($this->className); |
| 70 | + } catch (\ReflectionException $e) { |
| 71 | + // the class does not exist anymore |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + $hash = hash_init('md5'); |
| 76 | + |
| 77 | + foreach ($this->generateSignature($this->classReflector) as $info) { |
| 78 | + hash_update($hash, $info); |
| 79 | + } |
| 80 | + |
| 81 | + return hash_final($hash); |
| 82 | + } |
| 83 | + |
| 84 | + private function generateSignature(\ReflectionClass $class) |
| 85 | + { |
| 86 | + yield $class->getDocComment().$class->getModifiers(); |
| 87 | + yield print_r(class_parents($class->name), true); |
| 88 | + yield print_r(class_implements($class->name), true); |
| 89 | + yield print_r($class->getConstants(), true); |
| 90 | + |
| 91 | + $defaults = $class->getDefaultProperties(); |
| 92 | + |
| 93 | + foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $p) { |
| 94 | + yield $p->getDocComment().$p; |
| 95 | + yield print_r($defaults[$p->name], true); |
| 96 | + } |
| 97 | + |
| 98 | + if (defined('HHVM_VERSION')) { |
| 99 | + foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) { |
| 100 | + // workaround HHVM bug with variadics, see https://github.com/facebook/hhvm/issues/5762 |
| 101 | + yield preg_replace('/^ @@.*/m', '', new ReflectionMethodHhvmWrapper($m->class, $m->name)); |
| 102 | + } |
| 103 | + } else { |
| 104 | + foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) { |
| 105 | + yield preg_replace('/^ @@.*/m', '', $m); |
| 106 | + |
| 107 | + $defaults = array(); |
| 108 | + foreach ($m->getParameters() as $p) { |
| 109 | + $defaults[$p->name] = $p->isDefaultValueAvailable() ? $p->getDefaultValue() : null; |
| 110 | + } |
| 111 | + yield print_r($defaults, true); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @internal |
| 119 | + */ |
| 120 | +class ReflectionMethodHhvmWrapper extends \ReflectionMethod |
| 121 | +{ |
| 122 | + public function getParameters() |
| 123 | + { |
| 124 | + $params = array(); |
| 125 | + |
| 126 | + foreach (parent::getParameters() as $i => $p) { |
| 127 | + $params[] = new ReflectionParameterHhvmWrapper(array($this->class, $this->name), $i); |
| 128 | + } |
| 129 | + |
| 130 | + return $params; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * @internal |
| 136 | + */ |
| 137 | +class ReflectionParameterHhvmWrapper extends \ReflectionParameter |
| 138 | +{ |
| 139 | + public function getDefaultValue() |
| 140 | + { |
| 141 | + return array($this->isVariadic(), $this->isDefaultValueAvailable() ? parent::getDefaultValue() : null); |
| 142 | + } |
| 143 | +} |
0 commit comments