'Module "%s" has been generated!' ); protected $_errorMap = array( self::ERROR_INCORRECT_MODULE_NAME => 'Please set module name', self::ERROR_MODULE_IS_EXISTS => 'Module is exists. Please full remove old files' ); public function generate() { if (empty($this->_name)) { throw new Qs_Db_Exception($this->_errorMap[self::ERROR_INCORRECT_MODULE_NAME]); } if ($this->_checkModuleExists()) { throw new Qs_Db_Exception($this->_errorMap[self::ERROR_MODULE_IS_EXISTS]); } $this->_generateModuleFilesystem(); $this->_generateJS(); $this->_updateSitemapXML(); $this->_generateDB(); } protected function _generateModuleFilesystem() { $architecture = $this->_getArchitecture(); $buildDirectory = new App_Admin_Tool_Project_Context_BuildDirectory(); $buildFile = new App_Admin_Tool_Project_Context_BuildFile(); foreach ($architecture as $directory => $files) { $directoryPath = BASE_PATH . DIRECTORY_SEPARATOR . $directory; $buildDirectory->setFullPath($directoryPath); $buildDirectory->create(); foreach ($files as $file) { $buildFile->setBaseDirectory($directory); $buildFile->setFilesystemName($file); $buildFile->setModuleName($this->_name); $buildFile->generate(); } } return $this; } protected function _generateJS() { $filesystemName = strtolower($this->_name) . '.' . App_Admin_Tool_Project_Context_BuildFile::JS_FILE_EXTENSION; $buildFile = new App_Admin_Tool_Project_Context_BuildFile(); $buildFile->setBaseDirectory(self::MODULE_JS_APP_CONTAINER); $buildFile->setFilesystemName($filesystemName); $buildFile->setFullPath(WWW_PATH . '/' . self::MODULE_JS_APP_CONTAINER . '/' . $filesystemName); $buildFile->setModuleName($this->_name); $buildFile->generate(); } protected function _updateSitemapXML() { $moduleName = strtolower($this->_name); $sitemap = BASE_PATH . '/sitemap.xml'; $sitemapObj = new SimpleXMLElement($sitemap, 0, true); /** @var SimpleXMLElement $sitemapModuleContainer */ $sitemapModuleContainer = $sitemapObj->admin->sub; if (strlen($sitemapModuleContainer->{$moduleName}->getName())) { unset($sitemapModuleContainer->{$moduleName}); } $moduleSection = $sitemapModuleContainer->addChild($moduleName); $moduleSection->addAttribute('showInMenu', 'y'); $moduleSection->addAttribute('title', ucfirst($moduleName)); $moduleItem = $moduleSection->addChild('item'); $moduleItem->addAttribute('type', ucfirst($moduleName) . '_Admin_'); $sitemapObj->saveXML($sitemap); return $this; } protected function _generateDB() { $files = array( 'schema.sql', 'data.sql', ); $moduleName = ucfirst(strtolower($this->_name)); $db = Qs_Db::getInstance(); $dbConfig = Qs_Db::getConfig()->toArray(); $tableList = $db->listTables(); $tableName = $dbConfig['database']['tablePrefix'] . $moduleName; $tablePageItemType = $dbConfig['database']['tablePrefix'] . self::TABLE_PAGE_ITEM_TYPE; $itemType = $moduleName . '_'; $itemTitle = $moduleName; $itemSorter = (int) $db->fetchOne( $db->select()->from($tablePageItemType, new Zend_Db_Expr('MAX(sorter)')) ) + self::ITEM_TYPE_NEW_SORTER_STEP; if (in_array($tableName, $tableList)) { throw new Qs_Db_Exception('Table "' . $tableName . '" is exists'); } foreach ($files as $file) { $fullFilePath = dirname(__FILE__) . '/Module/Db/' . $file; if (file_exists($fullFilePath)) { try { $db->beginTransaction(); $query = ''; foreach (new SplFileObject($fullFilePath) as $line) { $params = compact('tableName', 'tablePageItemType', 'itemType', 'itemTitle', 'itemSorter'); $query .= str_replace( array_map('self::wrapSqlParams', array_keys($params)), array_values($params), $line ); if (substr(rtrim($query), -1) == ';') { $db->query($query); $query = ''; } } $db->commit(); } catch(Exception $e) { $db->rollback(); throw new Qs_Db_Exception($e->getMessage()); } } } return $this; } protected function _getArchitecture() { $moduleAppPath = self::MODULE_APP_CONTAINER . DIRECTORY_SEPARATOR . $this->getModuleName(); $moduleTplPath = self::MODULE_TPL_CONTAINER . DIRECTORY_SEPARATOR . $this->getModuleName(); return array( /** path: /App/MODULE_NAME */ $moduleAppPath => array( self::CONFIG_FILE_NAME, self::OBJ_FILE_NAME, self::VIEW_FILE_NAME, self::LIST_FILE_NAME ), /** path: /App/MODULE_NAME/Abstract */ $moduleAppPath . DIRECTORY_SEPARATOR . self::ABSTRACT_DIRECTORY_NAME => array( self::OBJ_FILE_NAME, self::VIEW_FILE_NAME, self::LIST_FILE_NAME ), /** path: /App/MODULE_NAME/Admin */ $moduleAppPath . DIRECTORY_SEPARATOR . self::ADMIN_DIRECTORY_NAME => array( self::OBJ_FILE_NAME, self::VIEW_FILE_NAME, self::LIST_FILE_NAME ), /** path: /App/MODULE_NAME/Admin/Form */ $moduleAppPath . DIRECTORY_SEPARATOR . self::ADMIN_DIRECTORY_NAME . DIRECTORY_SEPARATOR . self::FORM_DIRECTORY_NAME => array( self::ABSTRACT_FILE_NAME, self::NEW_FORM_FILE_NAME, self::EDIT_FORM_FILE_NAME, self::REORDER_FORM_FILE_NAME ), /** @todo languages */ /** path: /tpl/MODULE_NAME */ $moduleTplPath => array( self::LIST_TPL_FILE_NAME, self::VIEW_TPL_FILE_NAME ) ); } protected function _checkModuleExists() { $moduleContext = new App_Admin_Tool_Project_Context_BuildDirectory(array( 'baseDirectory' => self::MODULE_APP_CONTAINER, 'filesystemName' => $this->getModuleName() )); return $moduleContext->exists(); } public function getWorkflowAreaForm(array $options) { $formClassName = __CLASS__ . '_Form_Generate'; return new $formClassName($options); } public static function wrapSqlParams($value) { return '{{' . $value . '}}'; } public function setModuleName($name) { $this->_name = ucfirst(strtolower(preg_replace('/[^A-Za-z0-9]/i', '', $name))); return $this; } public function getModuleName() { return $this->_name; } public static function getMessage($key) { if (array_key_exists($key, self::$_messageMap)) { return self::$_messageMap[$key]; } return self::$_messageMap; } }