Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Prophecy.
|
Chris@0
|
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
6 * Marcello Duarte <marcello.duarte@gmail.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Prophecy\Doubler\ClassPatch;
|
Chris@0
|
13
|
Chris@0
|
14 use Prophecy\Doubler\Generator\Node\ClassNode;
|
Chris@0
|
15 use Prophecy\Doubler\Generator\Node\MethodNode;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * SplFileInfo patch.
|
Chris@0
|
19 * Makes SplFileInfo and derivative classes usable with Prophecy.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class SplFileInfoPatch implements ClassPatchInterface
|
Chris@0
|
24 {
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Supports everything that extends SplFileInfo.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param ClassNode $node
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return bool
|
Chris@0
|
31 */
|
Chris@0
|
32 public function supports(ClassNode $node)
|
Chris@0
|
33 {
|
Chris@0
|
34 if (null === $node->getParentClass()) {
|
Chris@0
|
35 return false;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 return 'SplFileInfo' === $node->getParentClass()
|
Chris@0
|
39 || is_subclass_of($node->getParentClass(), 'SplFileInfo')
|
Chris@0
|
40 ;
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Updated constructor code to call parent one with dummy file argument.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param ClassNode $node
|
Chris@0
|
47 */
|
Chris@0
|
48 public function apply(ClassNode $node)
|
Chris@0
|
49 {
|
Chris@0
|
50 if ($node->hasMethod('__construct')) {
|
Chris@0
|
51 $constructor = $node->getMethod('__construct');
|
Chris@0
|
52 } else {
|
Chris@0
|
53 $constructor = new MethodNode('__construct');
|
Chris@0
|
54 $node->addMethod($constructor);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 if ($this->nodeIsDirectoryIterator($node)) {
|
Chris@0
|
58 $constructor->setCode('return parent::__construct("' . __DIR__ . '");');
|
Chris@0
|
59
|
Chris@0
|
60 return;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 if ($this->nodeIsSplFileObject($node)) {
|
Chris@0
|
64 $constructor->setCode('return parent::__construct("' . __FILE__ .'");');
|
Chris@0
|
65
|
Chris@0
|
66 return;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 $constructor->useParentCode();
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Returns patch priority, which determines when patch will be applied.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @return int Priority number (higher - earlier)
|
Chris@0
|
76 */
|
Chris@0
|
77 public function getPriority()
|
Chris@0
|
78 {
|
Chris@0
|
79 return 50;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * @param ClassNode $node
|
Chris@0
|
84 * @return boolean
|
Chris@0
|
85 */
|
Chris@0
|
86 private function nodeIsDirectoryIterator(ClassNode $node)
|
Chris@0
|
87 {
|
Chris@0
|
88 $parent = $node->getParentClass();
|
Chris@0
|
89
|
Chris@0
|
90 return 'DirectoryIterator' === $parent
|
Chris@0
|
91 || is_subclass_of($parent, 'DirectoryIterator');
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * @param ClassNode $node
|
Chris@0
|
96 * @return boolean
|
Chris@0
|
97 */
|
Chris@0
|
98 private function nodeIsSplFileObject(ClassNode $node)
|
Chris@0
|
99 {
|
Chris@0
|
100 $parent = $node->getParentClass();
|
Chris@0
|
101
|
Chris@0
|
102 return 'SplFileObject' === $parent
|
Chris@0
|
103 || is_subclass_of($parent, 'SplFileObject');
|
Chris@0
|
104 }
|
Chris@0
|
105 }
|