1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Prophecy. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* Marcello Duarte <[email protected]> |
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 Prophecy\Doubler\ClassPatch; |
13
|
|
|
|
14
|
|
|
use Prophecy\Doubler\Generator\Node\ClassNode; |
15
|
|
|
use Prophecy\Doubler\Generator\Node\MethodNode; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* SplFileInfo patch. |
19
|
|
|
* Makes SplFileInfo and derivative classes usable with Prophecy. |
20
|
|
|
* |
21
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
22
|
|
|
*/ |
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
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->nodeIsSplFileObject($node)) { |
63
|
|
|
$constructor->setCode('return parent::__construct("' . __FILE__ .'");'); |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$constructor->useParentCode(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns patch priority, which determines when patch will be applied. |
72
|
|
|
* |
73
|
|
|
* @return int Priority number (higher - earlier) |
74
|
|
|
*/ |
75
|
|
|
public function getPriority() |
76
|
|
|
{ |
77
|
|
|
return 50; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ClassNode $node |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
|
|
private function nodeIsDirectoryIterator(ClassNode $node) |
85
|
|
|
{ |
86
|
|
|
$parent = $node->getParentClass(); |
87
|
|
|
return 'DirectoryIterator' === $parent |
88
|
|
|
|| is_subclass_of($parent, 'DirectoryIterator'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ClassNode $node |
93
|
|
|
* @return boolean |
94
|
|
|
*/ |
95
|
|
|
private function nodeIsSplFileObject(ClassNode $node) |
96
|
|
|
{ |
97
|
|
|
$parent = $node->getParentClass(); |
98
|
|
|
return 'SplFileObject' === $parent |
99
|
|
|
|| is_subclass_of($parent, 'SplFileObject'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|