1 | <?php |
||
23 | class SplFileInfoPatch implements ClassPatchInterface |
||
24 | { |
||
25 | /** |
||
26 | * Supports everything that extends SplFileInfo. |
||
27 | * |
||
28 | * @param ClassNode $node |
||
29 | * |
||
30 | * @return bool |
||
31 | */ |
||
32 | public function supports(ClassNode $node) |
||
33 | { |
||
34 | if (null === $node->getParentClass()) { |
||
35 | return false; |
||
36 | } |
||
37 | |||
38 | return 'SplFileInfo' === $node->getParentClass() |
||
39 | || is_subclass_of($node->getParentClass(), 'SplFileInfo') |
||
40 | ; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Updated constructor code to call parent one with dummy file argument. |
||
45 | * |
||
46 | * @param ClassNode $node |
||
47 | */ |
||
48 | public function apply(ClassNode $node) |
||
49 | { |
||
50 | if ($node->hasMethod('__construct')) { |
||
51 | $constructor = $node->getMethod('__construct'); |
||
52 | } else { |
||
53 | $constructor = new MethodNode('__construct'); |
||
54 | $node->addMethod($constructor); |
||
55 | } |
||
56 | |||
57 | if ($this->nodeIsDirectoryIterator($node)) { |
||
58 | $constructor->setCode('return parent::__construct("' . __DIR__ . '");'); |
||
59 | |||
60 | return; |
||
61 | } |
||
62 | |||
63 | if ($this->nodeIsSplFileObject($node)) { |
||
64 | $constructor->setCode('return parent::__construct("' . __FILE__ .'");'); |
||
65 | |||
66 | return; |
||
67 | } |
||
68 | |||
69 | $constructor->useParentCode(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns patch priority, which determines when patch will be applied. |
||
74 | * |
||
75 | * @return int Priority number (higher - earlier) |
||
76 | */ |
||
77 | public function getPriority() |
||
81 | |||
82 | /** |
||
83 | * @param ClassNode $node |
||
84 | * @return boolean |
||
85 | */ |
||
86 | private function nodeIsDirectoryIterator(ClassNode $node) |
||
93 | |||
94 | /** |
||
95 | * @param ClassNode $node |
||
96 | * @return boolean |
||
97 | */ |
||
98 | private function nodeIsSplFileObject(ClassNode $node) |
||
105 | } |
||
106 |