Last active
December 20, 2015 19:28
-
-
Save b3k/6182826 to your computer and use it in GitHub Desktop.
Magento CLI tool for creating new module/model etc.. still in dev progressUse: php bin/mado.php create_module community/Namespace/ModuleNamephp mado.php create_symlinks /target/absolute/path /source/absolute/path [--dry-run]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$mado = new Mado(); | |
try { | |
if (isset($argv[1]) && strlen($argv[1]) > 0) { | |
$mado->run($argv[1], array_slice($argv, 2)); | |
} else { | |
$mado->run('help', null); | |
} | |
} catch (Exception $e) { | |
Mado::flash('Exception throwed! ' . $e->getMessage()); | |
} | |
class Mado { | |
public function run($command, $args = array()) { | |
if (method_exists($this, strtolower($command))) { | |
$this->{strtolower($command)}($args); | |
} else { | |
throw new Exception('No command found!'); | |
} | |
} | |
/** | |
* @example php mago.php create_model community/Namespace/Module Model_Name | |
* @param type $args | |
*/ | |
public function create_model($args) { | |
$paths = explode('/', $args[0]); | |
$php_model_name = sprintf('%s_%s_Model_%s', ucfirst($paths[1]), ucfirst($paths[2]), ucfirst($args[1])); | |
$model_name = ucfirst($args[1]); | |
$base = dirname(__FILE__) . '/../'; | |
$afterCreateFile = function($path) { | |
call_user_func_array(array(__CLASS__, 'flash'), array('[+] File created: ' . $path)); | |
}; | |
self::createFileIfNotExists($base . 'app/code/' . $paths[0] . '/' . $paths[1] . '/' . $paths[2] . '/Model/' . $model_name, '', $afterCreateFile); | |
} | |
/** | |
* Refactor needed | |
* use: php mago.php create_module community/Namespace/Module | |
* @param type $args | |
* @throws Exception | |
*/ | |
public function create_module($args) { | |
$base = dirname(__FILE__) . '/../'; | |
self::flash('Creating module in: ' . $base); | |
$paths = explode('/', $args[0]); | |
if (count($paths) !== 3) { | |
throw new Exception('Bad path'); | |
} | |
$afterCreateDir = function($path) { | |
call_user_func_array(array(__CLASS__, 'flash'), array('[+] Dir created: ' . $path)); | |
}; | |
$afterCreateFile = function($path) { | |
call_user_func_array(array(__CLASS__, 'flash'), array('[+] File created: ' . $path)); | |
}; | |
$working_path = $base . 'app/code'; | |
foreach ($paths as $p) { | |
$working_path .= '/' . $p; | |
self::createDirIfNotExists($working_path, 0755, $afterCreateDir); | |
} | |
foreach (array('Helper', 'Model', 'Block', 'controllers', 'data', 'etc', 'sql') as $dir) { | |
self::createDirIfNotExists($working_path . '/' . $dir, 0755, $afterCreateDir); | |
} | |
self::createDirIfNotExists($working_path . '/data/' . strtolower($paths[1] . '_' . $paths[2]) . '_setup', 0755, $afterCreateDir); | |
self::createDirIfNotExists($working_path . '/sql/' . strtolower($paths[1] . '_' . $paths[2]) . '_setup', 0755, $afterCreateDir); | |
self::createFileIfNotExists($working_path . '/etc/config.xml', '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'app/design/frontend/base/default/template/' . strtolower($paths[1]), 0755, $afterCreateDir); | |
self::createFileIfNotExists($base . 'app/design/frontend/base/default/layout/' . $paths[1] . '_' . $paths[2] . '.xml', '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'app/design/frontend/base/default/template/' . strtolower($paths[1]), 0755, $afterCreateDir); | |
self::createFileIfNotExists($base . 'app/design/frontend/base/default/template/' . strtolower($paths[1]) . '/' . strtolower($paths[2]), '', $afterCreateFile); | |
self::createFileIfNotExists($base . 'skin/frontend/base/default/css/' . strtolower($paths[1] . '_' . $paths[2]) . '.css', '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'skin/frontend/base/default/js', 0755, $afterCreateDir); | |
self::createFileIfNotExists($base . 'skin/frontend/base/default/js/' . strtolower($paths[1] . '_' . $paths[2]) . '.js', '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'app/design/adminhtml/default/default/template/' . strtolower($paths[1]), 0755, $afterCreateDir); | |
self::createDirIfNotExists($base . 'skin/adminhtml/default/default/css', 0755, $afterCreateDir); | |
self::createFileIfNotExists($base . 'skin/adminhtml/default/default/css/' . strtolower($paths[1] . '_' . $paths[2]) . '.css', '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'skin/adminhtml/default/default/js', 0755, $afterCreateDir); | |
self::createFileIfNotExists($base . 'skin/adminhtml/default/default/js/' . strtolower($paths[1] . '_' . $paths[2]) . '.js', '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'app/design/frontend/base/default/template/' . strtolower($paths[1]), 0755, $afterCreateDir); | |
self::createFileIfNotExists($base . 'app/design/frontend/base/default/template/' . strtolower($paths[1]) . '/' . strtolower($paths[2]), '', $afterCreateFile); | |
self::createDirIfNotExists($base . 'app/design/adminhtml/default/default/template/' . strtolower($paths[1]), 0755, $afterCreateDir); | |
self::flash('Done!'); | |
} | |
public static function createDirIfNotExists($path, $p = 0755, callable $afterCreate = null) { | |
if (!file_exists($path)) { | |
mkdir($path, $p); | |
if (is_callable($afterCreate)) { | |
$afterCreate($path); | |
} | |
} | |
} | |
public static function createFileIfNotExists($path, $content = '', callable $afterCreate = null) { | |
if (!file_exists($path)) { | |
file_put_contents($path, $content); | |
if (is_callable($afterCreate)) { | |
$afterCreate($path); | |
} | |
} | |
} | |
public function help($argument = null) { | |
self::flash('Use: php mado.php create_module community/Namespace/ModuleName'); | |
self::flash('Use: php mado.php create_symlink /target/absolute/path /source/absolute/path [--dry-run]'); | |
} | |
public static function flash($msg) { | |
printf($msg . PHP_EOL, date('Y-m-d'), date('H:i:s')); | |
} | |
/** | |
* Creates symlinks | |
* | |
* use php mado.php create_symlinks /home/b3k/public_html/kinguin.local /var/www/kinguin.devel [--dry-run] | |
* | |
* @throws Exception | |
* @return boolean | |
*/ | |
public function create_symlinks($args) { | |
if (count($args) < 2 || !file_exists($args[0]) || !file_exists($args[1])) { | |
throw new Exception('Target or source dosent exists.'); | |
} | |
if (substr($args[0], 0, 1) !== '/' || substr($args[1], 0, 1) !== '/') { | |
throw new Exception('You have to provide abolute paths to source and target.'); | |
} | |
$target = $args[0]; | |
$source = $args[1]; | |
$created_dir_symlinks = array(); | |
$dirItr = new RecursiveDirectoryIterator($source); | |
$filterItr = new SymlinkSynchronizeRecursiveFilterIterator($dirItr); | |
$itr = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($itr as $filePath => $fileInfo) { | |
$in_target = str_replace($source, $target, $fileInfo->getRealPath()); | |
if (!file_exists($in_target)) { | |
if (!$fileInfo->isLink() && !self::coveredPathInArray($fileInfo->getRealPath(), $created_dir_symlinks)) { | |
if (!isset($args[2]) || $args[2] != '--dry-run') { | |
symlink($fileInfo->getRealPath(), $in_target); | |
} | |
if ($fileInfo->isDir()) { | |
$created_dir_symlinks[] = $fileInfo->getRealPath(); | |
} | |
self::flash('Create symlink: source: ' . $fileInfo->getRealPath() . ' - target: ' . $in_target); | |
} | |
} | |
} | |
} | |
public static function coveredPathInArray($path, $haystack) { | |
if (!is_array($haystack) || empty($haystack)) { | |
return false; | |
} | |
foreach ($haystack as $h) { | |
if (substr($path, 0, strlen($h)) == $h) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
class SymlinkSynchronizeRecursiveFilterIterator extends RecursiveFilterIterator { | |
// add other files to ignore | |
public static $filter_file = array('.git', '.svn'); | |
public function accept() { | |
return !in_array($this->current()->getFilename(), self::$filter_file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment