-
Notifications
You must be signed in to change notification settings - Fork 2
/
modman_files.php
391 lines (354 loc) · 12.8 KB
/
modman_files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* @copyright Copyright (c) 2013 The Magento Hackathon (UK)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Bolaji Olubajo <bolaji.tolulope@redboxdigital.com>
*/
include 'magehack' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'helper.php';
include 'magehack' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'generator.php';
include 'abstract.php';
class MageHack_Shell_Modman_Files extends Mage_Shell_Abstract
{
/**
* Required options
*
* @var array
*/
protected $_options = array(
'module_name' => array(
'required' => true,
'validator' => array(
'Zend_Validate_NotEmpty',
)),
'prefix' => array(
'required' => false,
'validator' => array(
'Zend_Validate_NotEmpty',
),
),
'strip' => array(
'required' => false,
'validator' => array(
'Zend_Validate_NotEmpty',
),
)
);
protected $_helper = NULL;
protected $_fileMappings = array();
protected $options = NULL;
public function run()
{
$options = $this->_getOptions();
if(!$options){
echo $this->usageHelp();
return;
}
$moduleName = $options->getModuleName();
if(!$this->_moduleExist($moduleName)){
echo $this->_cliRed(sprintf('Error: could not find specified module %s', $moduleName));
echo $this->usageHelp();
exit(0);
}
$prefix = $options->getPrefix();
$strip = ($options->getStrip()) ? $options->getStrip() : '';
$files = $this->_getAllFiles($moduleName);
if ($prefix) {
$this->_outputMappings($strip, $files, $prefix);
} else {
$this->_outputMappings($strip, $files);
}
}
/**
* Checks if a module exist
* @param string $moduleName should be in the form Namespace_ModuleName
* @return boolean
*/
protected function _moduleExist($moduleName)
{
$moduleConfigFile = Mage::getModuleDir('etc', $moduleName) . DS . 'config.xml';
if(is_readable($moduleConfigFile)){
return true;
}
return false;
}
/**
* Retuurn
* @param Mage_Core_Model_Layout_Element $config
* @return array
*/
protected function _getFrontLayoutUpdateFile(Mage_Core_Model_Layout_Element $config)
{
try {
return (array) $config->frontend->layout->updates;
} catch (Exception $e) {
return array();
}
}
/**
* Returns helper class
* @return MageHack_Shell_Modman_Helper
*/
protected function _getHelper()
{
if (!$this->_helper) {
$this->_helper = new MageHack_Shell_Modman_Helper();
}
return $this->_helper;
}
/**
* Returns module admin layout file if one exist
* @param Mage_Core_Model_Layout_Element $config
* @return array
*/
protected function _getAdminLayoutUpdateFile(Mage_Core_Model_Layout_Element $config)
{
try {
return (array) $config->adminhtml->layout->updates;
} catch (Exception $e) {
return array();
}
}
/**
* Returns module locale file if one exist
* @param Mage_Core_Model_Layout_Element $config
* @param string $area Magento design area
* @return array
*/
protected function _getLocaleFiles(Mage_Core_Model_Layout_Element $config, $moduleName, $area = 'frontend')
{
if (!isset($config->$area->translate->modules)) {
return array();
}
$nodes = $config->$area->translate->modules->children();
$baseLocaleDir = Mage::getBaseDir('locale');
if (isset($nodes)) {
foreach ($nodes as $moduleName => $info) {
$info = $info->asArray();
if (isset($info['files'])) {
$fileNames = $info['files'];
foreach ($fileNames as $fileName) {
$file = $baseLocaleDir . DS . Mage::app()->getLocale()->getLocaleCode() . DS . $fileName;
if (!is_readable($file)) {
$file = $baseLocaleDir . DS . 'en_US' . DS . $fileName;
}
return $this->_getHelper()->filterPath($file);
}
}
}
}
return array();
}
/**
* Generates all retrievable module files
* @param string $moduleName
* @return array
*/
protected function _getAllFiles($moduleName)
{
$configFile = file_get_contents(Mage::getModuleDir('etc', $moduleName) . DS . 'config.xml');
$config = simplexml_load_string(
$configFile, Mage::getConfig()->getModelClassName('core/layout_element')
);
$frontUpdateFiles = $this->_getFrontLayoutUpdateFile($config);
$generator = new MageHack_Shell_Modman_Generator();
$this->_addFilesToMappings($this->_getDefaultMappings($config, $moduleName));
$frontFileMappings = array();
if ($frontUpdateFiles) {
foreach ($frontUpdateFiles as $node) {
$generator->setMode('front');
$this->_addFilesToMappings($generator->getDesign()->getLayoutFilename((string) $node->file));
$generator->setDesignConfigXmlFile((string) $node->file);
$frontFileMappings = $this->_getHelper()->mergeArray($frontFileMappings, $generator->getMappings());
}
$this->_addFilesToMappings($frontFileMappings);
$this->_addFilesToMappings($this->_getHelper()->filterPath($this->_getLocaleFiles($config, $moduleName, 'frontend')));
}
$adminUpdateFiles = $this->_getAdminLayoutUpdateFile($config);
$adminFileMappings = array();
if ($adminUpdateFiles) {
$generator = new MageHack_Shell_Modman_Generator();
foreach ($adminUpdateFiles as $node) {
$generator->setMode('admin');
$this->_addFilesToMappings($generator->getDesign()->getLayoutFilename((string) $node->file));
$generator->setDesignConfigXmlFile((string) $node->file);
$this->_addFilesToMappings($generator->getMappings(1));
}
$adminFileMappings = $this->_getHelper()->mergeArray(
$adminFileMappings
, $this->_getHelper()->filterPath($this->_getLocaleFiles($config, $moduleName, 'admin'))
);
$this->_addFilesToMappings($adminFileMappings);
}
return $this->_fileMappings;
}
/**
* outputs module modman file mappings
* @param array $files
* @param string $prefix
*/
protected function _outputMappings($strip, $files, $prefix = '')
{
$files = $this->_getHelper()->getDirAndFiles($files);
foreach ($files as $actualfile) {
$tempfile = str_replace($strip, '', $actualfile);
$actual = $tempfile;
if (!empty($prefix)) {
$actual = preg_replace('#/{2,}#', '/', $prefix . DIRECTORY_SEPARATOR . $actual);
}
echo "$actual $actualfile" . PHP_EOL;
}
}
/**
* Returns the trivial module files e.g locale and module core files
* i.e in Namespace/ModuleName/app/code/local|community
* @param string $config
* @param string $moduleName
* @return array
*/
protected function _getDefaultMappings($config, $moduleName)
{
$data = array("app/etc/modules/$moduleName.xml");
$data = $this->_getHelper()->mergeArray($data, $this->_getModuleCoreFiles($moduleName));
$data = $this->_getHelper()->mergeArray($data, $this->_getLocaleFiles($config, $moduleName));
return $data;
}
/**
* Return module core files in Namespace/ModuleName/app/code/local|community
* @param type $moduleName
* @return type
*/
protected function _getModuleCoreFiles($moduleName)
{
$file = $this->_getHelper()->filterPath(dirname(Mage::getConfig()->getModuleDir('etc', $moduleName)));
return $file;
}
protected function _isEmpty($value)
{
return (empty($value) || !(int) $value > 0);
}
protected function _addFilesToMappings($fileName)
{
if(!is_array($fileName)){
$fileName = $this->_getHelper()->filterPath($fileName);
}
if(empty($fileName)){
return;
}
$this->_fileMappings = $this->_getHelper()->mergeArray($this->_fileMappings, $fileName);
return $this->_fileMappings;
}
/**
* Return array of CLI options
*
* @return array
* @author Bolaji Olubajo
*/
protected function _getCliOptions()
{
$cliOptions = array();
foreach ($_SERVER['argv'] as $arg) {
if (substr($arg, 0, 2) == '--' && strpos($arg, '=') !== false) {
$option = substr($arg, 2);
$option = explode('=', $option);
$cliOptions[$option[0]] = $option[1];
}
}
return $cliOptions;
}
/**
* Echo message in red colour
*
* @param string $message
* @return string
* @author Bolaji Olubajo <bolaji.olubajo@redboxdigital.com>
*/
protected function _cliRed($message)
{
return sprintf("\033[31m%s\033[37m", $message) . PHP_EOL;
;
}
/**
* Echo message in green colour
*
* @param string $message
* @return string
* @author Bolaji Olubajo <bolaji.olubajo@redboxdigital.com>
*/
protected function _cliGreen($message)
{
return sprintf("\033[32m%s\033[37m", $message) . PHP_EOL;
;
}
/**
* Get options
*
* @return false|Varien_Object
* @author Bolaji Olubajo <bolaji.olubajo@redboxdigital.com>
*/
protected function _getOptions()
{
if (empty($this->options)) {
$options = new Varien_Object();
$cliOptions = $this->_getCliOptions();
foreach ($this->_options as $optionId => $optionConfig) {
$option = array_key_exists($optionId, $cliOptions) ? $cliOptions[$optionId] : false;
/* Option is required */
if ($option === false && $optionConfig['required'] === true) {
echo sprintf($this->_cliRed("Option [%s] is required"), $optionId);
return false;
} else if ($option !== false) {
foreach ($optionConfig['validator'] as $validatorClass) {
/* Check if validator is available */
if (class_exists($validatorClass)) {
$validator = new $validatorClass;
} else {
echo sprintf($this->_cliRed("Validator [%s] cannot be invoked"), $validatorClass);
return false;
}
$validate = $option;
/* Get file real path if option is a filename */
if ($validator instanceof Zend_Validate_File_Exists) {
$validator->setDirectory(dirname(realpath($option)));
$validate = basename(realpath($option));
}
/* Required option did not pass validation */
if (!$validator->isValid($validate)) {
echo sprintf($this->_cliRed("Option [%s] did not pass validation [%s] for value [%s]"), $optionId, $validatorClass, $option);
return false;
}
/* Get file real path if option is a filename */
if ($validator instanceof Zend_Validate_File_Exists) {
$option = realpath($option);
}
}
}
$this->options = $options->setData($optionId, $option);
}
}
return $this->options;
}
protected function _prepareEnvironment()
{
set_time_limit(0);
ini_set('memory_limit', '512M');
}
/**
* Retrieve Usage Help Message
*
* @return string
* @author Bolaji Olubajo <bolaji.tolulope@redboxdigital.com>
*/
public function usageHelp()
{
return <<<USAGE
Generates file mappings for a Modman or Composer Magento module
Usage: php -f modman_files.php -- --module_name=Mage_Catalog --prefix="mycustom_dir"
Options:
--module_name Custom module name (REQUIRED) e.g Namespace_ModuleName
--prefix Your module module base directory from Magento root directory
--strip string to strip out of mapping
USAGE;
}
}
$shell = new MageHack_Shell_Modman_Files();
$shell->run();