1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c)2014-2014 heiglandreas |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category |
24
|
|
|
* @author Andreas Heigl<[email protected]> |
25
|
|
|
* @copyright ©2014-2014 Andreas Heigl |
26
|
|
|
* @license http://www.opesource.org/licenses/mit-license.php MIT-License |
27
|
|
|
* @version 0.0 |
28
|
|
|
* @since 06.11.14 |
29
|
|
|
* @link https://github.com/heiglandreas/ |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace Org_Heigl\FileFinder\Filter; |
33
|
|
|
|
34
|
|
|
use Org_Heigl\FileFinder\FilterInterface; |
35
|
|
|
|
36
|
|
|
class ClassIsInstanceof implements FilterInterface |
37
|
|
|
{ |
38
|
|
|
protected $instances = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check whether the given file should be included in the final Filelist |
42
|
|
|
* |
43
|
|
|
* @param \SplFileInfo $file |
44
|
|
|
* |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
4 |
|
public function filter(\SplFileInfo $file) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
4 |
|
$class = $this->getClassName($file->getPathname()); |
51
|
1 |
|
} catch (\Exception $e){ |
52
|
1 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
if (! @include_once($file->getPathname())) { |
56
|
1 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$ReflectionClass = new \ReflectionClass($class); |
60
|
|
|
|
61
|
2 |
|
foreach ($this->instances as $instance) { |
62
|
2 |
|
if ($ReflectionClass->isSubclassOf($instance)) { |
63
|
1 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
if ($class === $instance) { |
67
|
1 |
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
7 |
|
protected function getClassName($file) |
75
|
|
|
{ |
76
|
7 |
|
$content = new \Org_Heigl\FileFinder\Service\Tokenlist(file_get_contents($file)); |
77
|
7 |
|
$class = $content->getNamespace(); |
78
|
|
|
|
79
|
7 |
|
$classname = $content->getClassName(); |
80
|
7 |
|
if (! $classname) { |
81
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
82
|
2 |
|
'The given file "%s" does not contain a class', |
83
|
2 |
|
$file |
84
|
|
|
)); |
85
|
|
|
} |
86
|
5 |
|
unset($content); |
87
|
|
|
|
88
|
5 |
|
$class[] = $classname; |
89
|
|
|
|
90
|
5 |
|
$classname = str_replace('\\\\', '\\', '\\' . implode('\\', $class)); |
91
|
|
|
|
92
|
5 |
|
return $classname; |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
public function addInstance($instance) |
96
|
|
|
{ |
97
|
7 |
|
$this->instances[] = $instance; |
98
|
|
|
|
99
|
7 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
10 |
|
public function __construct($instances = array()) |
103
|
|
|
{ |
104
|
10 |
|
if (! is_array($instances)) { |
105
|
5 |
|
$instances = array($instances); |
106
|
|
|
} |
107
|
|
|
|
108
|
10 |
|
foreach($instances as $instance) { |
109
|
6 |
|
$this->addInstance($instance); |
110
|
|
|
} |
111
|
10 |
|
} |
112
|
|
|
} |
113
|
|
|
|