|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Script\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use N98\Util\OperatingSystem; |
|
6
|
|
|
use Symfony\Component\Finder\Finder; |
|
7
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
8
|
|
|
|
|
9
|
|
|
class ScriptLoader |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $homeDir; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $_scriptFiles = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
* @deprecated since 1.97.29 |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $_homeScriptFolder = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_magentoRootFolder = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_scriptFolders = array(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $scriptFolders |
|
39
|
|
|
* @param string $magentoRootFolder |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(array $scriptFolders, $magentoRootFolder = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->homeDir = OperatingSystem::getHomeDir(); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$this->_magentoRootFolder = $magentoRootFolder; |
|
46
|
|
|
|
|
47
|
|
|
if (OperatingSystem::isWindows()) { |
|
48
|
|
|
$scriptFolders[] = $this->homeDir . '/n98-magerun/scripts'; |
|
49
|
|
|
} |
|
50
|
|
|
$scriptFolders[] = $this->homeDir . '/.n98-magerun/scripts'; |
|
51
|
|
|
|
|
52
|
|
|
$this->findScripts($scriptFolders); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getFiles() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->_scriptFiles; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function findScripts(array $scriptFolders = null) |
|
64
|
|
|
{ |
|
65
|
|
|
if (null === $scriptFolders) { |
|
66
|
|
|
$scriptFolders = $this->_scriptFolders; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$scriptFolders = array_filter(array_filter($scriptFolders, 'strlen'), 'is_dir'); |
|
70
|
|
|
|
|
71
|
|
|
$this->_scriptFolders = $scriptFolders; |
|
72
|
|
|
$this->_scriptFiles = array(); |
|
73
|
|
|
if (1 > count($scriptFolders)) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$finder = Finder::create() |
|
78
|
|
|
->files() |
|
79
|
|
|
->followLinks(true) |
|
|
|
|
|
|
80
|
|
|
->ignoreUnreadableDirs(true) |
|
81
|
|
|
->name('*.magerun') |
|
82
|
|
|
->in($scriptFolders); |
|
83
|
|
|
|
|
84
|
|
|
$scriptFiles = array(); |
|
85
|
|
|
foreach ($finder as $file) { /* @var $file SplFileInfo */ |
|
86
|
|
|
$scriptFiles[$file->getFilename()] = array( |
|
87
|
|
|
'fileinfo' => $file, |
|
88
|
|
|
'description' => $this->_readFirstLineOfFile($file->getPathname()), |
|
89
|
|
|
'location' => $this->_getLocation($file->getPathname()), |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
ksort($scriptFiles); |
|
94
|
|
|
$this->_scriptFiles = $scriptFiles; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Reads the first line. If it's a comment return it. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $file |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function _readFirstLineOfFile($file) |
|
105
|
|
|
{ |
|
106
|
|
|
$f = @fopen($file, 'r'); |
|
107
|
|
|
if (!$f) { |
|
108
|
|
|
return ''; |
|
109
|
|
|
} |
|
110
|
|
|
$line = trim(fgets($f)); |
|
111
|
|
|
fclose($f); |
|
112
|
|
|
|
|
113
|
|
|
if (isset($line[0]) && $line[0] != '#') { |
|
114
|
|
|
return ''; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return trim(substr($line, 1)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $pathname |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function _getLocation($pathname) |
|
126
|
|
|
{ |
|
127
|
|
|
if (strstr($pathname, $this->_magentoRootFolder)) { |
|
128
|
|
|
return 'project'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (strstr($pathname, $this->homeDir)) { |
|
132
|
|
|
return 'personal'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (strstr($pathname, 'n98-magerun/modules')) { |
|
136
|
|
|
return 'module'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return 'system'; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.