├── README.markdown ├── Mtool ├── var │ └── license ├── Codegen │ ├── Template │ │ ├── module_config_empty.tpl │ │ ├── module_upgrader.tpl │ │ ├── module_etc.tpl │ │ ├── module_installer.tpl │ │ ├── block_blank.tpl │ │ ├── block_rewrite.tpl │ │ ├── helper_blank.tpl │ │ ├── model_blank.tpl │ │ ├── model_rewrite.tpl │ │ └── helper_rewrite.tpl │ ├── Exception │ │ ├── Config.php │ │ ├── Entity.php │ │ ├── Module.php │ │ └── Filesystem.php │ ├── Browser.php │ ├── Entity │ │ ├── Block.php │ │ ├── Model.php │ │ ├── Rmodel.php │ │ ├── Module │ │ │ └── Finder.php │ │ ├── Helper.php │ │ ├── Abstract.php │ │ └── Module.php │ ├── Parser.php │ ├── Template.php │ ├── Filesystem.php │ └── Config.php ├── Providers │ ├── Mtool.php │ ├── Rmodel.php │ ├── Block.php │ ├── Model.php │ ├── Helper.php │ ├── Module.php │ ├── Entity.php │ └── Abstract.php └── Magento.php └── MtoolManifest.php /README.markdown: -------------------------------------------------------------------------------- 1 | Mtool 2 | ======= 3 | 4 | Mtool is a magento code-genarator which should help magento-developers with their daily tasks. It uses zend tool framework. 5 | 6 | Documentation 7 | ------------ 8 | See in the [Wiki](https://github.com/dankocherga/MTool/wiki) 9 | -------------------------------------------------------------------------------- /Mtool/var/license: -------------------------------------------------------------------------------- 1 | @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 2 | This source file is subject to the Open Software License (OSL 3.0) 3 | that is bundled with this package in the file LICENSE.txt. 4 | It is also available through the world-wide-web at this URL: 5 | http://opensource.org/licenses/osl-3.0.php 6 | 7 | -------------------------------------------------------------------------------- /Mtool/Codegen/Template/module_config_empty.tpl: -------------------------------------------------------------------------------- 1 | 2 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Mtool/Codegen/Template/module_upgrader.tpl: -------------------------------------------------------------------------------- 1 | startSetup(); 30 | 31 | 32 | 33 | $installer->endSetup(); 34 | -------------------------------------------------------------------------------- /Mtool/Codegen/Exception/Config.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Exception_Config extends Exception 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /Mtool/Codegen/Exception/Entity.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Exception_Entity extends Exception 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /Mtool/Codegen/Exception/Module.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Exception_Module extends Exception 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /Mtool/Codegen/Exception/Filesystem.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Exception_Filesystem extends Exception 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /Mtool/Codegen/Template/module_etc.tpl: -------------------------------------------------------------------------------- 1 | 2 | 23 | 24 | 25 | <#{module_name}> 26 | true 27 | local 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Mtool/Codegen/Template/module_installer.tpl: -------------------------------------------------------------------------------- 1 | startSetup(); 30 | 31 | try { 32 | #code here... 33 | } catch (Exception $e) { 34 | Mage::logException($e); 35 | } 36 | $installer->endSetup(); 37 | -------------------------------------------------------------------------------- /Mtool/Codegen/Template/block_blank.tpl: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Codegen_Browser 26 | { 27 | /** 28 | * Find files in filesystem 29 | * 30 | * @param string $file 31 | * @param string $in - directory 32 | * @static 33 | * @return RegexIterator 34 | */ 35 | public static function find($file, $in) 36 | { 37 | $dir = new RecursiveDirectoryIterator($in); 38 | $iterator = new RecursiveIteratorIterator($dir); 39 | $regex = new RegexIterator($iterator, "/^.+{$file}$/i", RecursiveRegexIterator::GET_MATCH); 40 | return $regex; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Mtool/Providers/Mtool.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Mtool extends Mtool_Providers_Abstract 26 | { 27 | 28 | /** 29 | * Mage tool version 30 | * @var string 31 | */ 32 | protected $_version = '1.0.0'; 33 | 34 | /** 35 | * Get provider name 36 | * @return string 37 | */ 38 | public function getName() 39 | { 40 | return 'mtool'; 41 | } 42 | 43 | /** 44 | * Check mtool working 45 | */ 46 | public function info() 47 | { 48 | $this->_answer("Magento Command Line Console Tool v{$this->_version}"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Block.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Entity_Block extends Mtool_Codegen_Entity_Abstract 27 | { 28 | /** 29 | * Entity folder name 30 | * @var string 31 | */ 32 | protected $_folderName = 'Block'; 33 | 34 | /** 35 | * Create template name 36 | * @var string 37 | */ 38 | protected $_createTemplate = 'block_blank'; 39 | 40 | /** 41 | * Rewrite template name 42 | * @var string 43 | */ 44 | protected $_rewriteTemplate = 'block_rewrite'; 45 | 46 | /** 47 | * Entity name 48 | * @var string 49 | */ 50 | protected $_entityName = 'Block'; 51 | 52 | /** 53 | * Namespace in config file 54 | * @var string 55 | */ 56 | protected $_configNamespace = 'blocks'; 57 | } 58 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Model.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Entity_Model extends Mtool_Codegen_Entity_Abstract 27 | { 28 | /** 29 | * Entity folder name 30 | * @var string 31 | */ 32 | protected $_folderName = 'Model'; 33 | 34 | /** 35 | * Create template name 36 | * @var string 37 | */ 38 | protected $_createTemplate = 'model_blank'; 39 | 40 | /** 41 | * Rewrite template name 42 | * @var string 43 | */ 44 | protected $_rewriteTemplate = 'model_rewrite'; 45 | 46 | /** 47 | * Entity name 48 | * @var string 49 | */ 50 | protected $_entityName = 'Model'; 51 | 52 | /** 53 | * Namespace in config file 54 | * @var string 55 | */ 56 | protected $_configNamespace = 'models'; 57 | } 58 | -------------------------------------------------------------------------------- /MtoolManifest.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class MtoolManifest implements Zend_Tool_Framework_Manifest_ProviderManifestable 26 | { 27 | /** 28 | * Register autoload for the tool 29 | */ 30 | public function __construct() 31 | { 32 | $autoloader = Zend_Loader_Autoloader::getInstance(); 33 | $autoloader->registerNamespace('Mtool_'); 34 | } 35 | 36 | /** 37 | * Get available providers 38 | * @return array 39 | */ 40 | public function getProviders() 41 | { 42 | return array( 43 | new Mtool_Providers_Mtool(), 44 | new Mtool_Providers_Module(), 45 | new Mtool_Providers_Model(), 46 | new Mtool_Providers_Rmodel(), 47 | new Mtool_Providers_Helper(), 48 | new Mtool_Providers_Block(), 49 | ); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Mtool/Providers/Rmodel.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Rmodel extends Mtool_Providers_Entity 26 | { 27 | /** 28 | * Get provider name 29 | * @return string 30 | */ 31 | public function getName() 32 | { 33 | return 'mage-rmodel'; 34 | } 35 | 36 | /** 37 | * Rewrite model 38 | * 39 | * @param string $targetModule in format of companyname/modulename 40 | * @param string $originModel in format of catalog/product 41 | * @param string $yourModel in format of catalog_product 42 | */ 43 | public function rewrite($targetModule = null, $originModel = null, $yourModel = null) 44 | { 45 | $this->_rewriteEntity(new Mtool_Codegen_Entity_Rmodel(), 'resource model', $targetModule, $originModel, 46 | $yourModel); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Mtool/Codegen/Parser.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Codegen_Parser 26 | { 27 | /** 28 | * Template params 29 | * @var array 30 | */ 31 | protected $_params = array(); 32 | 33 | /** 34 | * Set template params 35 | * @param array $params 36 | * @return Mtool_Codegen_Template 37 | */ 38 | public function setParams($params) 39 | { 40 | $this->_params = $params; 41 | return $this; 42 | } 43 | 44 | /** 45 | * Parse content and inject parmeters 46 | * parameter keys are encolured in the #{} mask 47 | * 48 | * @param string $content 49 | * @access public 50 | * @return string 51 | */ 52 | public function parse($content) 53 | { 54 | $pattern = '/(#\{(.*?)\})/'; 55 | return preg_replace_callback($pattern, array($this, 'parseVar'), $content); 56 | } 57 | 58 | /** 59 | * Parse one var 60 | * 61 | * @param array $matches 62 | * @return string 63 | */ 64 | protected function parseVar($matches) 65 | { 66 | return @$this->_params[@$matches[2]]; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Mtool/Codegen/Template.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Codegen_Template extends Mtool_Codegen_Parser 26 | { 27 | /** 28 | * Template name 29 | * @var string 30 | */ 31 | protected $_template; 32 | 33 | /** 34 | * Init template 35 | * 36 | * @param string $name 37 | * should be the same as filename from the Template directory 38 | * witout extension 39 | */ 40 | public function __construct($name) 41 | { 42 | $this->_template = $name; 43 | } 44 | 45 | /** 46 | * Move template to $to directory with $as name 47 | * 48 | * @param string $to - target directory 49 | * @param string $as - result filename 50 | */ 51 | public function move($to, $as) 52 | { 53 | $to = Mtool_Codegen_Filesystem::slash($to); 54 | Mtool_Codegen_Filesystem::mkdir($to); 55 | Mtool_Codegen_Filesystem::write( 56 | $to . $as, 57 | $this->content()); 58 | } 59 | 60 | 61 | /** 62 | * Get parsed template content 63 | * @return string 64 | */ 65 | public function content() 66 | { 67 | $templatePath = dirname(__FILE__) . 68 | DIRECTORY_SEPARATOR . 'Template' . 69 | DIRECTORY_SEPARATOR . "{$this->_template}.tpl"; 70 | $source = Mtool_Codegen_Filesystem::read($templatePath); 71 | return $this->parse($source); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Rmodel.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Entity_Rmodel extends Mtool_Codegen_Entity_Model 27 | { 28 | /** 29 | * Rewrite resource model 30 | * 31 | * @param string $originNamespace 32 | * @param string $originPath 33 | * @param string $path 34 | * @param Mtool_Codegen_Entity_Module $module 35 | */ 36 | public function rewrite($originNamespace, $originPath, $path, Mtool_Codegen_Entity_Module $module) 37 | { 38 | // Find origin class prefix 39 | $resourceModel = $this->lookupOriginEntityClass($originNamespace, $module->findThroughModules('config.xml'), 'resourceModel'); 40 | $classPrefix = $this->lookupOriginEntityClass($resourceModel, $module->findThroughModules('config.xml')); 41 | 42 | // Create own class 43 | $originPathSteps = $this->_ucPath(explode('_', $originPath)); 44 | $originClassName = implode('_', $originPathSteps); 45 | $params = array( 46 | 'original_class_name' => "{$classPrefix}_{$originClassName}" 47 | ); 48 | $className = $this->createClass($path, $this->_rewriteTemplate, $module, $params); 49 | 50 | //Register rewrite in config 51 | $config = new Mtool_Codegen_Config($module->getConfigPath('config.xml')); 52 | $config->set("global/{$this->_configNamespace}/{$resourceModel}/rewrite/{$originPath}", $className); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Module/Finder.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Entity_Module_Finder 27 | { 28 | /** 29 | * Find module by entity namespace 30 | * 31 | * @param string $root - path to the working directory 32 | * @param Mtool_Codegen_Entity_Abstract $entity 33 | * @param string $namespace - namespace name 34 | * @param array $templateConfig 35 | * @return Mtool_Codegen_Entity_Module | null 36 | */ 37 | public static function byNamespace($root, $entity, $namespace, array $templateConfig) 38 | { 39 | $configNamespace = $entity->getConfigNamespace(); 40 | $mage = new Mtool_Magento($root); 41 | 42 | // Find all local config.xml files 43 | $configs = $mage->findInCode('config.xml', $mage->getCodepoolPath()); 44 | try 45 | { 46 | // Find entity definition through local modules 47 | $classPrefix = $entity->lookupOriginEntityClass($namespace, $configs); 48 | 49 | // Extract module and company names from class prefix 50 | list($companyName, $moduleName) = explode('_', $classPrefix); 51 | return new Mtool_Codegen_Entity_Module($root, $moduleName, $companyName, $templateConfig); 52 | } 53 | catch(Mtool_Codegen_Exception_Entity $e) 54 | { 55 | return null; 56 | } 57 | catch(Exception $e) 58 | { 59 | throw $e; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Mtool/Providers/Block.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Block extends Mtool_Providers_Entity 26 | { 27 | /** 28 | * Get provider name 29 | * @return string 30 | */ 31 | public function getName() 32 | { 33 | return 'mage-block'; 34 | } 35 | 36 | /** 37 | * Create block 38 | * 39 | * @param string $targetModule in format of companyname/modulename 40 | * @param string $blockPath in format of mymodule/block_path 41 | */ 42 | public function create($targetModule = null, $blockPath = null) 43 | { 44 | $this->_createEntity(new Mtool_Codegen_Entity_Block(), 'block', $targetModule, $blockPath); 45 | } 46 | 47 | /** 48 | * Create new block with module auto-guessing 49 | * @param string $blockPath in format of mymodule/block_path 50 | */ 51 | public function add($blockPath = null) 52 | { 53 | $this->_createEntityWithAutoguess(new Mtool_Codegen_Entity_Block(), 'block', $blockPath); 54 | } 55 | 56 | /** 57 | * Rewrite block 58 | * 59 | * @param string $targetModule in format of companyname/modulename 60 | * @param string $originBlock in format of catalog/product 61 | * @param string $yourBlock in format of catalog_product 62 | */ 63 | public function rewrite($targetModule = null, $originBlock = null, $yourBlock = null) 64 | { 65 | $this->_rewriteEntity(new Mtool_Codegen_Entity_Block(), 'block', $targetModule, $originBlock, $yourBlock); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Mtool/Providers/Model.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Model extends Mtool_Providers_Entity 26 | { 27 | /** 28 | * Get provider name 29 | * @return string 30 | */ 31 | public function getName() 32 | { 33 | return 'mage-model'; 34 | } 35 | 36 | /** 37 | * Create model 38 | * 39 | * @param string $targetModule in format of companyname/modulename 40 | * @param string $modelPath in format of mymodule/model_path 41 | */ 42 | public function create($targetModule = null, $modelPath = null) 43 | { 44 | $this->_createEntity(new Mtool_Codegen_Entity_Model(), 'model', $targetModule, $modelPath); 45 | } 46 | 47 | /** 48 | * Create new model with module auto-guessing 49 | * @param string $modelPath in format of mymodule/model_path 50 | */ 51 | public function add($modelPath = null) 52 | { 53 | $this->_createEntityWithAutoguess(new Mtool_Codegen_Entity_Model(), 'model', $modelPath); 54 | } 55 | 56 | /** 57 | * Rewrite model 58 | * 59 | * @param string $targetModule in format of companyname/modulename 60 | * @param string $originModel in format of catalog/product 61 | * @param string $yourModel in format of catalog_product 62 | */ 63 | public function rewrite($targetModule = null, $originModel = null, $yourModel = null) 64 | { 65 | $this->_rewriteEntity(new Mtool_Codegen_Entity_Model(), 'model', $targetModule, $originModel, $yourModel); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Mtool/Providers/Helper.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Helper extends Mtool_Providers_Entity 26 | { 27 | /** 28 | * Get provider name 29 | * @return string 30 | */ 31 | public function getName() 32 | { 33 | return 'mage-helper'; 34 | } 35 | 36 | /** 37 | * Create helper 38 | * 39 | * @param string $targetModule in format of companyname/modulename 40 | * @param string $helperPath in format of mymodule/helper_path 41 | */ 42 | public function create($targetModule = null, $helperPath = null) 43 | { 44 | $this->_createEntity(new Mtool_Codegen_Entity_Helper(), 'helper', $targetModule, $helperPath); 45 | } 46 | 47 | /** 48 | * Create new helper with module auto-guessing 49 | * @param string $helperPath in format of mymodule/helper_path 50 | */ 51 | public function add($helperPath = null) 52 | { 53 | $this->_createEntityWithAutoguess(new Mtool_Codegen_Entity_Helper(), 'helper', $helperPath); 54 | } 55 | 56 | /** 57 | * Rewrite helper 58 | * 59 | * @param string $targetModule in format of companyname/modulename 60 | * @param string $originHelper in format of catalog/product 61 | * @param string $yourHelper in format of catalog_product 62 | */ 63 | public function rewrite($targetModule = null, $originHelper = null, $yourHelper = null) 64 | { 65 | $this->_rewriteEntity(new Mtool_Codegen_Entity_Helper(), 'helper', $targetModule, $originHelper, $yourHelper); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Helper.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Entity_Helper extends Mtool_Codegen_Entity_Abstract 27 | { 28 | /** 29 | * Entity folder name 30 | * @var string 31 | */ 32 | protected $_folderName = 'Helper'; 33 | 34 | /** 35 | * Create template name 36 | * @var string 37 | */ 38 | protected $_createTemplate = 'helper_blank'; 39 | 40 | /** 41 | * Rewrite template name 42 | * @var string 43 | */ 44 | protected $_rewriteTemplate = 'helper_rewrite'; 45 | 46 | /** 47 | * Entity name 48 | * @var string 49 | */ 50 | protected $_entityName = 'Helper'; 51 | 52 | /** 53 | * Namespace in config file 54 | * @var string 55 | */ 56 | protected $_configNamespace = 'helpers'; 57 | 58 | /** 59 | * Rewrite Magento entity. Helpers have specific behavior - no alias in core configs. 60 | * 61 | * @param string $originNamespace 62 | * @param string $originPath 63 | * @param string $path 64 | * @param Mtool_Codegen_Entity_Module $module 65 | */ 66 | public function rewrite($originNamespace, $originPath, $path, Mtool_Codegen_Entity_Module $module) 67 | { 68 | // Create own class 69 | $originPathSteps = $this->_ucPath(explode('_', $originPath)); 70 | $originModuleName = ucfirst($originNamespace); 71 | $originClassName = implode('_', $originPathSteps); 72 | $params = array( 73 | 'original_class_name' => "Mage_{$originModuleName}_{$this->_entityName}_{$originClassName}" 74 | ); 75 | $className = $this->createClass($path, $this->_rewriteTemplate, $module, $params); 76 | 77 | // Register rewrite in config 78 | $config = new Mtool_Codegen_Config($module->getConfigPath('config.xml')); 79 | $config->set("global/{$this->_configNamespace}/{$originNamespace}/rewrite/{$originPath}", $className); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Mtool/Magento.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Magento 26 | { 27 | /** 28 | * Magento root path with a slash on the ending 29 | * 30 | * @var string 31 | */ 32 | protected $_root; 33 | 34 | /** 35 | * Configure path to magento 36 | * 37 | * @param string $path - absolute path tp magento root 38 | */ 39 | public function __construct($root) 40 | { 41 | $this->_root = Mtool_Codegen_Filesystem::slash($root); 42 | } 43 | 44 | /** 45 | * Get Magento codepool path 46 | * 47 | * @param string $pool (local/community) 48 | * @return string 49 | */ 50 | public function getCodepoolPath($pool = 'local') 51 | { 52 | return $this->_root . 'app' . 53 | DIRECTORY_SEPARATOR . 'code' . 54 | DIRECTORY_SEPARATOR . $pool . 55 | DIRECTORY_SEPARATOR; 56 | } 57 | 58 | /** 59 | * Get Magento modules config path 60 | * 61 | * @param string $pool (local/community) 62 | * @return string 63 | */ 64 | public function getModulesConfigPath() 65 | { 66 | return $this->_root . 'app' . 67 | DIRECTORY_SEPARATOR . 'etc' . 68 | DIRECTORY_SEPARATOR . 'modules' . 69 | DIRECTORY_SEPARATOR; 70 | } 71 | 72 | /** 73 | * Find file in the code pools 74 | * 75 | * @param string $search 76 | * @param string|null $where 77 | * @return RegexIterator 78 | */ 79 | public function findInCode($search, $where = null) 80 | { 81 | if($where === null) 82 | $where = $this->_root . 'app' . DIRECTORY_SEPARATOR . 'code'; 83 | return Mtool_Codegen_Browser::find($search, $where); 84 | } 85 | 86 | /** 87 | * Get user's home directory path 88 | * 89 | * @return string 90 | */ 91 | public static function getHomeDir() 92 | { 93 | return $_SERVER['HOME']; 94 | } 95 | 96 | /** 97 | * Get project root directory path 98 | * 99 | * @return string 100 | */ 101 | public static function getRoot() 102 | { 103 | return rtrim(Mtool_Codegen_Filesystem::slash(getcwd()), '/'); 104 | } 105 | 106 | /** 107 | * Get Mtool directory path 108 | * 109 | * @return string 110 | */ 111 | public static function getMtoolDir() 112 | { 113 | return dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'Mtool'; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Mtool/Codegen/Filesystem.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Codegen_Filesystem 26 | { 27 | /** 28 | * Recursively create dir with 0777 permissions 29 | * 30 | * @static 31 | * @param string $path 32 | * @throws Mtool_Codegen_Exception_Filesystem 33 | */ 34 | public static function mkdir($path) 35 | { 36 | if(is_dir($path)) { 37 | return; 38 | } 39 | 40 | $result = mkdir($path, 0777, true); 41 | if($result === false) { 42 | throw new Mtool_Codegen_Exception_Filesystem( 43 | "Cannot create directory {$path}. Maybe permissions problem?" 44 | ); 45 | } 46 | system('chmod -R 755 ' . $path); 47 | } 48 | 49 | /** 50 | * Read file contents 51 | * 52 | * @param string $filepath 53 | * @static 54 | * @return string 55 | */ 56 | public static function read($filepath) 57 | { 58 | $handle = fopen($filepath, 'r'); 59 | $result = fread($handle, filesize($filepath)); 60 | if($result === false) { 61 | throw new Mtool_Codegen_Exception_Filesystem( 62 | "Cannot read file {$filepath}" 63 | ); 64 | } 65 | return $result; 66 | } 67 | 68 | /** 69 | * Write into file 70 | * 71 | * @param string $filepath 72 | * @param string $content 73 | * @static 74 | * @return string 75 | */ 76 | public static function write($filepath, $content) 77 | { 78 | $handle = fopen($filepath, 'w+'); 79 | $result = fwrite($handle, $content); 80 | if($result === false) { 81 | throw new Mtool_Codegen_Exception_Filesystem( 82 | "Cannot write into file {$filepath}" 83 | ); 84 | } 85 | system('chmod -R 644 ' . $filepath); 86 | return $result; 87 | } 88 | 89 | /** 90 | * Check if file exists 91 | * 92 | * @param string $path 93 | * @static 94 | * @return boolean 95 | */ 96 | public static function exists($path) 97 | { 98 | return file_exists($path); 99 | } 100 | 101 | /** 102 | * Add diretory separator 103 | * to the end of the path if it's not there 104 | * 105 | * @param string $path 106 | * @static 107 | * @return string 108 | */ 109 | public static function slash($path) 110 | { 111 | if($path[strlen($path) - 1] != DIRECTORY_SEPARATOR) { 112 | $path .= DIRECTORY_SEPARATOR; 113 | } 114 | return $path; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Mtool/Providers/Module.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Module extends Mtool_Providers_Abstract 26 | { 27 | /** 28 | * Get provider name 29 | * @return string 30 | */ 31 | public function getName() 32 | { 33 | return 'mage-module'; 34 | } 35 | 36 | /** 37 | * Create module 38 | * @param string $name in format of companyname/modulename 39 | */ 40 | public function create($name = null) 41 | { 42 | if ($name == null || false === strpos($name, '/')) { 43 | $companyName = $this->_ask('Enter the company name'); 44 | $moduleName = $this->_ask('Enter the module name'); 45 | } 46 | else list($companyName, $moduleName) = explode('/', $name); 47 | 48 | $module = new Mtool_Codegen_Entity_Module(getcwd(), $moduleName, $companyName, $this->_getConfig()); 49 | $module->createDummy(); 50 | 51 | $this->_answer('Done'); 52 | } 53 | 54 | /** 55 | * Create module installer 56 | * 57 | * @param string $name in format of companyname/modulename 58 | * @param string $version - initial module version in format of 1.5.7 59 | */ 60 | public function install($name = null, $version = null) 61 | { 62 | if ($name == null) { 63 | $name = $this->_ask('Enter the target module (like Mycompany/Mymodule)'); 64 | } 65 | if ($version == null) { 66 | $version = $this->_ask('Enter the initial module version (like 1.0.0)'); 67 | } 68 | list($companyName, $moduleName) = explode('/', $name); 69 | $module = new Mtool_Codegen_Entity_Module(getcwd(), $moduleName, $companyName, $this->_getConfig()); 70 | 71 | $module->install($version); 72 | $this->_answer('Done'); 73 | } 74 | 75 | /** 76 | * Create module upgrader 77 | * 78 | * @param string $name 79 | * @param string $mode 80 | * @param string $version 81 | * @return void 82 | */ 83 | public function upgrade($name = null, $mode = null, $version = null) 84 | { 85 | if ($name == null) { 86 | $name = $this->_ask('Enter the target module (like Mycompany/Mymodule)'); 87 | } 88 | if ($mode == null) { 89 | $mode = $this->_ask('How to upgrade - to exact version or increment existing? (enter "' . 90 | Mtool_Codegen_Entity_Module::UPGRADE_MODE_EXACT . '" or "' . 91 | Mtool_Codegen_Entity_Module::UPGRADE_MODE_INCREMENT . '")'); 92 | } 93 | if ($version == null) { 94 | switch ($mode) { 95 | case Mtool_Codegen_Entity_Module::UPGRADE_MODE_EXACT: 96 | $version = $this->_ask('Enter the module version (like 1.0.0)'); 97 | break; 98 | case Mtool_Codegen_Entity_Module::UPGRADE_MODE_INCREMENT: 99 | $version = $this->_ask('Enter the increment mask (like *.*.1 , * means same value as now)'); 100 | break; 101 | } 102 | } 103 | 104 | list($companyName, $moduleName) = explode('/', $name); 105 | $module = new Mtool_Codegen_Entity_Module(getcwd(), $moduleName, $companyName, $this->_getConfig()); 106 | 107 | $module->upgrade($mode, $version); 108 | $this->_answer('Done'); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /Mtool/Providers/Entity.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Providers_Entity extends Mtool_Providers_Abstract 26 | { 27 | /** 28 | * Create entity 29 | * 30 | * @param Mtool_Codegen_Entity_Abstract $entity 31 | * @param string $name 32 | * @param string $targetModule in format of companyname/modulename 33 | * @param string $entityPath in format of mymodule/model_path 34 | */ 35 | protected function _createEntity($entity, $name, $targetModule = null, $entityPath = null) 36 | { 37 | if ($targetModule == null) { 38 | $targetModule = $this->_ask('Enter the target module (in format of Mycompany/Mymodule)'); 39 | } 40 | if ($entityPath == null) { 41 | $entityPath = $this->_ask("Enter the {$name} path (in format of mymodule/{$name}_path)"); 42 | } 43 | 44 | list($companyName, $moduleName) = explode('/', $targetModule); 45 | 46 | $module = new Mtool_Codegen_Entity_Module(getcwd(), $moduleName, $companyName, $this->_getConfig()); 47 | 48 | list($namespace, $entityName) = explode('/', $entityPath); 49 | 50 | $entity->create($namespace, $entityName, $module); 51 | 52 | $this->_answer('Done'); 53 | } 54 | 55 | /** 56 | * Create entity with target module autoguessing 57 | * 58 | * @param Mtool_Codegen_Entity_Abstract $entity 59 | * @param string $name 60 | * @param string $entityPath in format of mymodule/model_path 61 | */ 62 | protected function _createEntityWithAutoguess($entity, $name, $entityPath = null) 63 | { 64 | if ($entityPath == null) { 65 | $entityPath = $this->_ask("Enter the {$name} path (in format of mymodule/{$name}_path)"); 66 | } 67 | list($namespace, $entityName) = explode('/', $entityPath); 68 | 69 | $module = Mtool_Codegen_Entity_Module_Finder::byNamespace(getcwd(), $entity, $namespace, $this->_getConfig()); 70 | if ($module == null) { 71 | $targetModule = $this->_ask("Unfortunately module with {$name} namespace '{$namespace}' was not found. Enter the target module (in format of Mycompany/Mymodule)"); 72 | list($companyName, $moduleName) = explode('/', $targetModule); 73 | $module = new Mtool_Codegen_Entity_Module(getcwd(), $moduleName, $companyName, $this->_getConfig()); 74 | } 75 | 76 | $entity->create($namespace, $entityName, $module); 77 | $this->_answer("New {$name} created under {$module->getName()} module"); 78 | } 79 | 80 | /** 81 | * Rewrite entity 82 | * 83 | * @param Mtool_Codegen_Entity_Abstract $entity 84 | * @param string $name 85 | */ 86 | protected function _rewriteEntity($entity, $name, $targetModule = null, $originEntityPath = null, $entityName = null) 87 | { 88 | if ($targetModule == null) { 89 | $targetModule = $this->_ask('Enter the target module (in format of Mycompany/Mymodule)'); 90 | } 91 | if ($originEntityPath == null) { 92 | $originEntityPath = $this->_ask("Enter the magento {$name} path (for example catalog/product_type_simple)"); 93 | } 94 | if ($entityName == null) { 95 | $entityName = $this->_ask("Enter your {$name} path (without namespace, in format of {$name}_path)"); 96 | } 97 | 98 | list($companyName, $moduleName) = explode('/', $targetModule); 99 | $module = new Mtool_Codegen_Entity_Module(getcwd(), $moduleName, $companyName, $this->_getConfig()); 100 | 101 | list($originNamespace, $originEntityName) = explode('/', $originEntityPath); 102 | 103 | $entity->rewrite($originNamespace, $originEntityName, $entityName, $module); 104 | 105 | $this->_answer('Done'); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /Mtool/Codegen/Config.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class Mtool_Codegen_Config 26 | { 27 | /** 28 | * Xml handle 29 | * @var SimpleXMLElement 30 | */ 31 | protected $_xml; 32 | 33 | /** 34 | * Config path 35 | * @var string 36 | */ 37 | protected $_path; 38 | 39 | /** 40 | * Load config 41 | * @param string $filepath 42 | */ 43 | public function __construct($filepath) 44 | { 45 | libxml_use_internal_errors(true); 46 | if(!Mtool_Codegen_Filesystem::exists($filepath)) 47 | throw new Mtool_Codegen_Exception_Config("Config file does not exist: {$filepath}"); 48 | 49 | $this->_xml = simplexml_load_file($filepath); 50 | if($this->_xml === false) 51 | { 52 | $message = "Cannot load config file: {$filepath}"; 53 | foreach(libxml_get_errors() as $_error) 54 | $message .= "; {$_error->message}"; 55 | throw new Mtool_Codegen_Exception_Config($message); 56 | } 57 | 58 | $this->_path = $filepath; 59 | } 60 | 61 | /** 62 | * Set config value 63 | * 64 | * @param string $path separated by slash (/) 65 | * @param string $value 66 | */ 67 | public function set($path, $value) 68 | { 69 | $segments = explode('/', $path); 70 | $node = $this->_xml; 71 | foreach($segments as $_key => $_segment) 72 | { 73 | if(!$node->$_segment->getName()) 74 | $node->addChild($_segment); 75 | 76 | if($_key == count($segments) - 1) 77 | $node->$_segment = $value; 78 | 79 | $node = $node->$_segment; 80 | } 81 | 82 | Mtool_Codegen_Filesystem::write($this->_path, $this->asPrettyXML()); 83 | } 84 | 85 | /** 86 | * Format xml with indents and line breaks 87 | * 88 | * @return string 89 | * @author Gary Malcolm 90 | */ 91 | public function asPrettyXML() 92 | { 93 | $string = $this->_xml->asXML(); 94 | 95 | // put each element on it's own line 96 | $string =preg_replace("/>\s*\n<",$string); 97 | 98 | // each element to own array 99 | $xmlArray = explode("\n",$string); 100 | 101 | // holds indentation 102 | $currIndent = 0; 103 | 104 | $indent = " "; 105 | // set xml element first by shifting of initial element 106 | $string = array_shift($xmlArray) . "\n"; 107 | foreach($xmlArray as $element) 108 | { 109 | // find open only tags... add name to stack, and print to string 110 | // increment currIndent 111 | if (preg_match('/^<([\w])+[^>\/]*>$/U',$element)) 112 | { 113 | $string .= str_repeat($indent, $currIndent) . $element . "\n"; 114 | $currIndent += 1; 115 | } // find standalone closures, decrement currindent, print to string 116 | elseif ( preg_match('/^<\/.+>$/',$element)) 117 | { 118 | $currIndent -= 1; 119 | $string .= str_repeat($indent, $currIndent) . $element . "\n"; 120 | } // find open/closed tags on the same line print to string 121 | else 122 | $string .= str_repeat($indent, $currIndent) . $element . "\n"; 123 | } 124 | return $string; 125 | } 126 | 127 | /** 128 | * Read the config value 129 | * 130 | * @param string $path 131 | * @return string 132 | */ 133 | public function get($path) 134 | { 135 | $node = $this->_xml; 136 | foreach(explode('/', $path) as $_segment) 137 | if($node->$_segment) 138 | $node = $node->$_segment; 139 | 140 | return (string) trim($node); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Abstract.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | abstract class Mtool_Codegen_Entity_Abstract 27 | { 28 | const RECOMMENDED_ZEND_CODING_STANDARD_LINE_LENGTH = 80; 29 | 30 | /** 31 | * Entity folder name 32 | * @var string 33 | */ 34 | protected $_folderName; 35 | 36 | /** 37 | * Create template name 38 | * @var string 39 | */ 40 | protected $_createTemplate; 41 | 42 | /** 43 | * Rewrite template name 44 | * @var string 45 | */ 46 | protected $_rewriteTemplate; 47 | 48 | /** 49 | * Entity name 50 | * @var string 51 | */ 52 | protected $_entityName; 53 | 54 | /** 55 | * Namespace in config file 56 | * @var string 57 | */ 58 | protected $_configNamespace; 59 | 60 | /** 61 | * Get entity config namespace 62 | * @return string 63 | */ 64 | public function getConfigNamespace() 65 | { 66 | return $this->_configNamespace; 67 | } 68 | 69 | /** 70 | * Create new entity 71 | * 72 | * @param string $namespace 73 | * @param string $path 74 | * @param Mtool_Codegen_Entity_Module $module 75 | */ 76 | public function create($namespace, $path, Mtool_Codegen_Entity_Module $module) 77 | { 78 | // Create class file 79 | $this->createClass($path, $this->_createTemplate, $module); 80 | 81 | // Create namespace in config if not exist 82 | $config = new Mtool_Codegen_Config($module->getConfigPath('config.xml')); 83 | $configPath = "global/{$this->_configNamespace}/{$namespace}/class"; 84 | if (!$config->get($configPath)) { 85 | $config->set($configPath, "{$module->getName()}_{$this->_entityName}"); 86 | } 87 | } 88 | 89 | /** 90 | * Rewrite entity 91 | * 92 | * @param string $originNamespace 93 | * @param string $originPath 94 | * @param string $path 95 | * @param Mtool_Codegen_Entity_Module $module 96 | */ 97 | public function rewrite($originNamespace, $originPath, $path, Mtool_Codegen_Entity_Module $module) 98 | { 99 | // Find origin class prefix 100 | $classPrefix = $this->lookupOriginEntityClass($originNamespace, $module->findThroughModules('config.xml')); 101 | 102 | // Create own class 103 | $originPathSteps = $this->_ucPath(explode('_', $originPath)); 104 | $originClassName = implode('_', $originPathSteps); 105 | $params = array( 106 | 'original_class_name' => "{$classPrefix}_{$originClassName}" 107 | ); 108 | $className = $this->createClass($path, $this->_rewriteTemplate, $module, $params); 109 | 110 | //Register rewrite in config 111 | $config = new Mtool_Codegen_Config($module->getConfigPath('config.xml')); 112 | $config->set("global/{$this->_configNamespace}/{$originNamespace}/rewrite/{$originPath}", $className); 113 | } 114 | 115 | /** 116 | * Lookup the class definition among the project 117 | * modules 118 | * 119 | * @param string $namespace 120 | * @param RegexIterator $configs 121 | * @param string $field 122 | * @return string (like Mage_Catalog_Model) 123 | */ 124 | public function lookupOriginEntityClass($namespace, $configs, $field = 'class') 125 | { 126 | foreach ($configs as $_config) { 127 | try { 128 | $config = new Mtool_Codegen_Config(@$_config[0]); 129 | if ($prefix = $config->get("global/{$this->_configNamespace}/{$namespace}/{$field}")) return $prefix; 130 | } catch (Exception $e) { 131 | 132 | } 133 | } 134 | 135 | throw new Mtool_Codegen_Exception_Entity("Module with namespace {$namespace} not found"); 136 | } 137 | 138 | /** 139 | * Create class file 140 | * 141 | * @param string $path in format: class_path_string 142 | * @param string $template 143 | * @param Mtool_Codegen_Entity_Module $module 144 | * @param array $params 145 | * @return resulting class name 146 | */ 147 | public function createClass($path, $template, $module, $params = array()) 148 | { 149 | $pathSteps = $this->_ucPath(explode('_', $path)); 150 | $className = implode('_', $pathSteps); 151 | $classFilename = array_pop($pathSteps) . '.php'; 152 | 153 | // Create class dir under module 154 | $classDir = Mtool_Codegen_Filesystem::slash($module->getDir()) . $this->_folderName . 155 | DIRECTORY_SEPARATOR . 156 | implode(DIRECTORY_SEPARATOR, $pathSteps); 157 | Mtool_Codegen_Filesystem::mkdir($classDir); 158 | 159 | // Move class template file 160 | $classTemplate = new Mtool_Codegen_Template($template); 161 | $resultingClassName = "{$module->getName()}_{$this->_entityName}_{$className}"; 162 | 163 | if (strlen('class ' . $resultingClassName) >= self::RECOMMENDED_ZEND_CODING_STANDARD_LINE_LENGTH) { 164 | $resultingClassName .= "\n "; 165 | } 166 | 167 | $defaultParams = array( 168 | 'company_name' => $module->getCompanyName(), 169 | 'module_name' => $module->getModuleName(), 170 | 'class_name' => $resultingClassName, 171 | 'year' => date('Y'), 172 | ); 173 | 174 | $classTemplate->setParams(array_merge($defaultParams, $params, $module->getTemplateParams())); 175 | $classTemplate 176 | ->move($classDir, $classFilename); 177 | 178 | return $resultingClassName; 179 | } 180 | 181 | /** 182 | * Uppercase path steps 183 | * 184 | * @param array $steps 185 | * @return array 186 | */ 187 | protected function _ucPath($steps) 188 | { 189 | $result = array(); 190 | foreach ($steps as $_step) { 191 | $result[] = ucfirst($_step); 192 | } 193 | return $result; 194 | } 195 | 196 | } 197 | -------------------------------------------------------------------------------- /Mtool/Providers/Abstract.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | abstract class Mtool_Providers_Abstract extends Zend_Tool_Framework_Provider_Abstract 26 | { 27 | /** 28 | * Config file name 29 | */ 30 | const CONFIG_FILE_NAME = '.mtool.ini'; 31 | 32 | /** 33 | * Create configs for project 34 | * 35 | * Configs will be created by asking user to answer some questions 36 | * and saved in the config file (~/.mtool.ini) 37 | * 38 | * @param string $configFileName config file name, basically it's ~/.mtool.ini 39 | * @return null 40 | */ 41 | protected function _createConfig($configFileName) 42 | { 43 | // id of the last project 44 | $maxProjectId = 0; 45 | 46 | // create config file if it's not exists 47 | if (!file_exists($configFileName)) { 48 | if (!$configFileHandle = fopen($configFileName, 'a+')) { 49 | throw new Mtool_Codegen_Exception_Filesystem( 50 | "Cannot create config file {$configFileName}. Maybe permissions problem?" 51 | ); 52 | } else { 53 | fclose($configFileHandle); 54 | } 55 | } 56 | 57 | // if file already exists, try to find other projects configs 58 | $iniConfig = new Zend_Config_Ini($configFileName, null, array( 59 | 'skipExtends' => true, 60 | 'allowModifications' => true 61 | )); 62 | 63 | if (!is_null($iniConfig->projects)) { 64 | $projects = $iniConfig->projects->toArray(); 65 | $maxProjectId = max(array_keys($projects)); 66 | } 67 | 68 | // access to _ask()/_anwer() methods 69 | $author = $this->_ask("Please, enter data for the @author string\n" 70 | . "For example, Dan Kocherga " 71 | ); 72 | 73 | $copyright = $this->_ask("Please, enter data for the copyright owner\n" 74 | . "For example, Oggetto Web ltd (http://oggettoweb.com/)" 75 | ); 76 | 77 | $licensePath = $this->_ask("Please, enter path to the license file\n" 78 | . "For example, /home/user/project/license.lic\n" 79 | . "Or press Enter to use the same as in the Magento" 80 | ); 81 | 82 | $moduleOwner = $this->_ask( 83 | "Please, enter module owner\n" 84 | . "For example, Oggetto Web\n" 85 | . "It will be used to generate a short file description string" 86 | ); 87 | 88 | $projectId = $maxProjectId + 1; 89 | 90 | $newProject = array( 91 | $projectId => array( 92 | 'copyright_company' => $copyright, 93 | 'path' => Mtool_Magento::getRoot(), 94 | 'author' => $author, 95 | 'module_owner' => $moduleOwner, 96 | ) 97 | ); 98 | 99 | if ($licensePath) { 100 | $newProject[$projectId]['license_path'] = $licensePath; 101 | } 102 | 103 | if (!isset($projects) || !is_array($projects)) { 104 | $projects = array(); 105 | } 106 | 107 | $iniConfig->projects = array_merge($projects, $newProject); 108 | 109 | // save configurations to the config file 110 | $options = array( 111 | 'config' => $iniConfig, 112 | 'filename' => $configFileName, 113 | ); 114 | $writer = new Zend_Config_Writer_Ini($options); 115 | $writer->write(); 116 | 117 | return $iniConfig; 118 | } 119 | 120 | /** 121 | * Get params from config file 122 | * 123 | * @throws Mtool_Codegen_Exception_Filesystem 124 | * @return array 125 | */ 126 | protected function _getConfig() 127 | { 128 | $configFile = Mtool_Magento::getHomeDir() . DIRECTORY_SEPARATOR . self::CONFIG_FILE_NAME; 129 | 130 | try { 131 | $iniConfig = new Zend_Config_Ini($configFile); 132 | } catch (Zend_Config_Exception $e) { 133 | $iniConfig = $this->_createConfig($configFile); 134 | } 135 | 136 | if ((is_null($iniConfig->projects)) 137 | || !($iniConfig->projects instanceof Zend_Config) 138 | ) { 139 | // no 'projects' in the config file 140 | $iniConfig = $this->_createConfig($configFile); 141 | } else { 142 | $projectConfigId = null; 143 | // find id of current project 144 | foreach ($iniConfig->projects->toArray() as $key => $_projectConfig) { 145 | if (is_array($_projectConfig)) { 146 | if ((isset($_projectConfig['path'])) 147 | && ($_projectConfig['path'] == Mtool_Magento::getRoot()) 148 | ) { 149 | $projectConfigId = $key; 150 | break; 151 | } 152 | } 153 | } 154 | 155 | if (!is_null($projectConfigId)) { 156 | $configs = $iniConfig->projects->{$projectConfigId}->toArray(); 157 | } else { 158 | // no config for the current project 159 | $iniConfig = $this->_createConfig($configFile); 160 | } 161 | } 162 | if (!isset($configs['license_path'])) { 163 | $configs['license_path'] = ''; 164 | } 165 | list($configs['license'], $configs['license_short']) = $this->_getLicenseStrings($configs['license_path']); 166 | return $configs; 167 | } 168 | 169 | /** 170 | * Retrurns License text with the asterisk before each string 171 | * 172 | * @return string 173 | */ 174 | protected function _getLicenseStrings($filename = '') 175 | { 176 | if (is_readable($filename)) { 177 | $strings = file($filename); 178 | } else { 179 | $strings = file(Mtool_Magento::getMtoolDir() . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'license', 180 | FILE_IGNORE_NEW_LINES); 181 | } 182 | 183 | for ($i = 0; $i < count($strings); $i++) { 184 | if (preg_match('/^@license\s+(.*)/', $strings[$i], $matchs)) { 185 | $licenseShort = $matchs[1]; 186 | unset($strings[$i]); 187 | continue; 188 | } 189 | if (strlen($strings[$i])) { 190 | $text[] = $strings[$i] . "\n"; 191 | } 192 | } 193 | 194 | return array(rtrim(' * ' . implode(' * ', $text), "\n"), $licenseShort); 195 | } 196 | 197 | /** 198 | * Ask user about 199 | * 200 | * @param string $question 201 | * @return string 202 | */ 203 | protected function _ask($question) 204 | { 205 | $response = $this->_registry 206 | ->getClient() 207 | ->promptInteractiveInput($question); 208 | return $response->getContent(); 209 | } 210 | 211 | /** 212 | * Pass answer to the output 213 | * @param string $text 214 | */ 215 | protected function _answer($text) 216 | { 217 | $this->_registry->getResponse() 218 | ->appendContent($text); 219 | } 220 | 221 | } 222 | 223 | -------------------------------------------------------------------------------- /Mtool/Codegen/Entity/Module.php: -------------------------------------------------------------------------------- 1 | 25 | */ 26 | class Mtool_Codegen_Entity_Module extends Mtool_Codegen_Entity_Abstract 27 | { 28 | /* 29 | * Upgrade modes 30 | */ 31 | const UPGRADE_MODE_EXACT = 'to'; 32 | const UPGRADE_MODE_INCREMENT = 'inc'; 33 | 34 | /** 35 | * Magento configuration class 36 | * 37 | * @var Mtool_Magento 38 | */ 39 | protected $_mage; 40 | 41 | /** 42 | * Module name 43 | * @var string 44 | */ 45 | protected $_moduleName; 46 | 47 | /** 48 | * Company name 49 | * @var string 50 | */ 51 | protected $_companyName; 52 | 53 | /** 54 | * Module dir path 55 | * @var string 56 | */ 57 | protected $_moduleDir; 58 | 59 | /** 60 | * Module configs (etc) dir path 61 | * @var string 62 | */ 63 | protected $_moduleConfigsDir; 64 | 65 | /** 66 | * Module sql dir path 67 | * @var string 68 | */ 69 | protected $_moduleSqlDir; 70 | 71 | /** 72 | * Params to substitude in templates 73 | * 74 | * @var array 75 | */ 76 | protected $_templateParams = array(); 77 | 78 | /** 79 | * Init environemnt 80 | * 81 | * @param string $root absolute path to magento root 82 | * @param string $moduleName 83 | * @param string $companyName 84 | * @param array $params (such as author, license etc.) to substitude in templates 85 | */ 86 | public function __construct($root, $moduleName, $companyName, array $params) 87 | { 88 | $this->_mage = new Mtool_Magento($root); 89 | 90 | $this->_moduleName = ucfirst($moduleName); 91 | $this->_companyName = ucfirst($companyName); 92 | $this->_templateParams = $params; 93 | 94 | $this->_moduleDir = $this->_mage->getCodepoolPath() 95 | . $this->_companyName 96 | . DIRECTORY_SEPARATOR 97 | . $this->_moduleName 98 | ; 99 | 100 | $this->_moduleConfigsDir = $this->_moduleDir . DIRECTORY_SEPARATOR . 'etc'; 101 | $this->_moduleSqlDir = $this->_moduleDir . DIRECTORY_SEPARATOR . 'sql'; 102 | } 103 | 104 | /** 105 | * Create dummy module: 106 | * 1. create module folder under app/code/local 107 | * 2. create module config.xml file 108 | * 3. create module file under app/etc/modules 109 | */ 110 | public function createDummy() 111 | { 112 | // Check that module does not already exist 113 | if ($this->exists()) 114 | throw new Mtool_Codegen_Exception_Module( 115 | "Seems like this module already exists. Aborting." 116 | ); 117 | 118 | // Create module dir 119 | Mtool_Codegen_Filesystem::mkdir($this->_moduleDir); 120 | 121 | $name = $this->getName(); 122 | 123 | $params = array( 124 | 'module_name' => $name, 125 | 'module' => $this->_moduleName, 126 | 'company_name' => $this->getCompanyName(), 127 | 'year' => date('Y'), 128 | ); 129 | 130 | // Create config.xml file 131 | $configTemplate = new Mtool_Codegen_Template('module_config_empty'); 132 | $configTemplate 133 | ->setParams(array_merge($params, $this->_templateParams)) 134 | ->move($this->_moduleConfigsDir, 'config.xml'); 135 | 136 | // Create module file under app/etc/modules 137 | $modulesTemplate = new Mtool_Codegen_Template('module_etc'); 138 | $modulesTemplate 139 | ->setParams(array_merge($params, $this->_templateParams)) 140 | ->move($this->_mage->getModulesConfigPath(), "{$name}.xml"); 141 | } 142 | 143 | /** 144 | * Create module installer: 145 | * 1. create version entry in the config 146 | * 2. create setup resource entry in config 147 | * 3. create installer file under module sql directory 148 | * 149 | * @param string $version - initial version in format 1.0.0 150 | */ 151 | public function install($version) 152 | { 153 | if (!$this->exists()) 154 | throw new Mtool_Codegen_Exception_Module( 155 | "Seems like this module does not exist. Aborting." 156 | ); 157 | 158 | $config = new Mtool_Codegen_Config($this->getConfigPath('config.xml')); 159 | 160 | // Create version entry in config 161 | $config->set("modules/{$this->getName()}/version", $version); 162 | 163 | // Create setup resource entry in config 164 | $setupNamspace = strtolower($this->getName()) . '_setup'; 165 | $config->set("global/resources/{$setupNamspace}/setup/module", $this->getName()); 166 | $config->set("global/resources/{$setupNamspace}/setup/connection", 'core_setup'); 167 | 168 | // Create installer file 169 | $modulesTemplate = new Mtool_Codegen_Template('module_installer'); 170 | 171 | $params = array( 172 | 'module_name' => $this->getName(), 173 | 'module' => $this->_moduleName, 174 | 'company_name' => $this->getCompanyName(), 175 | 'year' => date('Y'), 176 | 'class' => ($config->get("global/resources/{$setupNamspace}/setup/class")) 177 | ? : 'Mage_Core_Model_Resource_Setup', 178 | ); 179 | 180 | $modulesTemplate 181 | ->setParams(array_merge($params, $this->_templateParams)) 182 | ->move($this->_moduleSqlDir . DIRECTORY_SEPARATOR . $setupNamspace, 183 | "mysql4-install-{$version}.php"); 184 | } 185 | 186 | /** 187 | * Upgrade magento module 188 | * 189 | * @param string $mode - see mode constants 190 | * @param string $versionRequest - exact version or a mask depending on mode 191 | */ 192 | public function upgrade($mode, $versionRequest) 193 | { 194 | if (!$this->exists()) 195 | throw new Mtool_Codegen_Exception_Module( 196 | "Seems like this module does not exist. Aborting." 197 | ); 198 | 199 | $config = new Mtool_Codegen_Config($this->getConfigPath('config.xml')); 200 | 201 | // Define version value 202 | $currentVersion = $config->get("modules/{$this->getName()}/version"); 203 | switch ($mode) { 204 | case self::UPGRADE_MODE_EXACT: 205 | $version = $versionRequest; 206 | break; 207 | case self::UPGRADE_MODE_INCREMENT: 208 | $version = $this->_forceVersion($currentVersion, $versionRequest); 209 | break; 210 | default: 211 | throw new Mtool_Codegen_Exception_Module( 212 | "Undefined version upgrade type: {$mode}" 213 | ); 214 | } 215 | $config->set("modules/{$this->getName()}/version", $version); 216 | 217 | // Create upgrade file 218 | $setupNamspace = strtolower($this->getName()) . '_setup'; 219 | $modulesTemplate = new Mtool_Codegen_Template('module_upgrader'); 220 | 221 | $params = array( 222 | 'module_name' => $this->getName(), 223 | 'module' => $this->_moduleName, 224 | 'company_name' => $this->getCompanyName(), 225 | 'year' => date('Y'), 226 | 'class' => ($config->get("global/resources/{$setupNamspace}/setup/class")) 227 | ? : 'Mage_Core_Model_Resource_Setup', 228 | ); 229 | 230 | $modulesTemplate 231 | ->setParams(array_merge($params, $this->_templateParams)) 232 | ->move($this->_moduleSqlDir . DIRECTORY_SEPARATOR . $setupNamspace, 233 | "mysql4-upgrade-{$currentVersion}-{$version}.php"); 234 | } 235 | 236 | /** 237 | * Force version with increment by mask 238 | * 239 | * @param string $version input value 240 | * @param string $mask in format of *.*.1 241 | * where * means same value as in input 242 | * @return string 243 | */ 244 | protected function _forceVersion($version, $mask) 245 | { 246 | $maskSegments = explode('.', $mask); 247 | $versionSegments = explode('.', $version); 248 | foreach ($versionSegments as $_index => &$_segment) 249 | if (isset($maskSegments[$_index]) && $maskSegments[$_index] != '*') $_segment += $maskSegments[$_index]; 250 | 251 | return implode('.', $versionSegments); 252 | } 253 | 254 | /** 255 | * Check if module exists 256 | * @return boolean 257 | */ 258 | public function exists() 259 | { 260 | // Decide by existance of config.xml file 261 | return Mtool_Codegen_Filesystem::exists($this->_moduleConfigsDir . DIRECTORY_SEPARATOR . 'config.xml'); 262 | } 263 | 264 | /** 265 | * Get module dir path 266 | * @return string 267 | */ 268 | public function getDir() 269 | { 270 | return $this->_moduleDir; 271 | } 272 | 273 | /** 274 | * Get path to config file 275 | * 276 | * @param string $file with .xml 277 | * @return string 278 | */ 279 | public function getConfigPath($file) 280 | { 281 | return $this->_moduleConfigsDir . DIRECTORY_SEPARATOR . $file; 282 | } 283 | 284 | /** 285 | * Get module name in format 286 | * Company_Module 287 | * 288 | * @return string 289 | */ 290 | public function getName() 291 | { 292 | return "{$this->_companyName}_{$this->_moduleName}"; 293 | } 294 | 295 | /** 296 | * Get company name 297 | * @return string 298 | */ 299 | public function getCompanyName() 300 | { 301 | return $this->_companyName; 302 | } 303 | 304 | /** 305 | * Get module name 306 | * @return string 307 | */ 308 | public function getModuleName() 309 | { 310 | return $this->_moduleName; 311 | } 312 | 313 | /** 314 | * Find file through modules 315 | * 316 | * @param string $search 317 | * @return RegexIterator 318 | */ 319 | public function findThroughModules($search) 320 | { 321 | return $this->_mage->findInCode($search); 322 | } 323 | 324 | /** 325 | * Get params to substitude in templates 326 | * 327 | * @return array 328 | */ 329 | public function getTemplateParams() 330 | { 331 | return $this->_templateParams; 332 | } 333 | 334 | } 335 | --------------------------------------------------------------------------------