From ef712e22fa8d4375cdc3d9d8d937b72097977965 Mon Sep 17 00:00:00 2001 From: dMetzger Date: Tue, 21 Aug 2012 08:33:54 +0000 Subject: [PATCH] // install / uninstall of override file from modules fixed --- classes/module/Module.php | 52 +++++++++++++++++++++++----- install-dev/upgrade/sql/1.5.0.15.sql | 6 ++-- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/classes/module/Module.php b/classes/module/Module.php index 327c446a7..1db5eb19f 100644 --- a/classes/module/Module.php +++ b/classes/module/Module.php @@ -215,8 +215,13 @@ abstract class ModuleCore } // Install overrides - if (!$this->installOverrides()) + try { + $this->installOverrides(); + } catch (Exception $e) { + $this->_errors[] = Tools::displayError('Unable to install override:').' '.$e->getMessage(); + $this->uninstallOverrides(); return false; + } // Install module and retrieve the installation id $result = Db::getInstance()->insert($this->table, array('name' => $this->name, 'active' => 1, 'version' => $this->version)); @@ -1850,24 +1855,40 @@ abstract class ModuleCore { $path = Autoload::getInstance()->getClassPath($classname.'Core'); + // Check if there is already an override file, if not, we just need to copy the file + if (!($classpath = Autoload::getInstance()->getClassPath($classname))) + { + $override_src = $this->getLocalPath().'override'.DIRECTORY_SEPARATOR.$path; + $override_dest = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'override'.DIRECTORY_SEPARATOR.$path; + if (!is_writable(dirname($override_dest))) + throw new Exception(sprintf(Tools::displayError('directory (%s) not writable'), dirname($override_dest))); + copy($override_src, $override_dest); + return true; + } + // Check if override file is writable $override_path = _PS_ROOT_DIR_.'/'.Autoload::getInstance()->getClassPath($classname); if (!is_writable($override_path)) - return false; - + throw new Exception(sprintf(Tools::displayError('file (%s) not writable'), $override_path)); + // Make a reflection of the override class and the module override class $override_file = file($override_path); eval(preg_replace(array('#^\s*<\?php#', '#class\s+'.$classname.'\s+extends\s+([a-z0-9_]+)(\s+implements\s+([a-z0-9_]+))?#i'), array('', 'class '.$classname.'OverrideOriginal'), implode('', $override_file))); $override_class = new ReflectionClass($classname.'OverrideOriginal'); - $module_file = file($this->getLocalPath().'override/'.$path); + $module_file = file($this->getLocalPath().'override'.DIRECTORY_SEPARATOR.$path); eval(preg_replace(array('#^\s*<\?php#', '#class\s+'.$classname.'(\s+extends\s+([a-z0-9_]+)(\s+implements\s+([a-z0-9_]+))?)?#i'), array('', 'class '.$classname.'Override'), implode('', $module_file))); $module_class = new ReflectionClass($classname.'Override'); // Check if none of the methods already exists in the override class foreach ($module_class->getMethods() as $method) - if ($override_class->hasMethod($method->name)) - return false; + if ($override_class->hasMethod($method->getName())) + throw new Exception(sprintf(Tools::displayError('the method %s in the class %s is already overriden'), $method->getName(), $classname)); + + // Check if none of the properties already exists in the override class + foreach ($module_class->getProperties() as $property) + if ($override_class->hasProperty($property->getName())) + throw new Exception(sprintf(Tools::displayError('the property %s in the class %s is already defined'), $property->getName(), $classname)); // Insert the methods from module override in override $copy_from = array_slice($module_file, $module_class->getStartLine() + 1, $module_class->getEndLine() - $module_class->getStartLine() - 2); @@ -1906,14 +1927,29 @@ abstract class ModuleCore $override_file = file($override_path); foreach ($module_class->getMethods() as $method) { - if (!$override_class->hasMethod($method->name)) + if (!$override_class->hasMethod($method->getName())) continue; - $method = $override_class->getMethod($method->name); + $method = $override_class->getMethod($method->getName()); $length = $method->getEndLine() - $method->getStartLine() + 1; array_splice($override_file, $method->getStartLine() - 1, $length, array_pad(array(), $length, '#--remove--#')); } + // Remove properties from override file + foreach ($module_class->getProperties() as $property) + { + if (!$override_class->hasProperty($property->getName())) + continue; + + // Remplacer la ligne de déclaration par "remove" + foreach ($override_file as $line_number => &$line_content) + if (preg_match('/(public|private|protected)\s+(static\s+)?\$'.$property->getName().'/i', $line_content)) + { + $line_content = '#--remove--#'; + break; + } + } + // Rewrite nice code $code = ''; foreach ($override_file as $line) diff --git a/install-dev/upgrade/sql/1.5.0.15.sql b/install-dev/upgrade/sql/1.5.0.15.sql index da473ee1d..d05ef0ec3 100644 --- a/install-dev/upgrade/sql/1.5.0.15.sql +++ b/install-dev/upgrade/sql/1.5.0.15.sql @@ -4,8 +4,8 @@ SET NAMES 'utf8'; ALTER TABLE `PREFIX_order_state` ADD `module_name` VARCHAR(255) NULL DEFAULT NULL AFTER `send_email`; -UPDATE `PREFIX_order_state` SET `module_name`='cheque' WHERE `id_order_state`=(SELECT `value` FROM `PREFIX_configuration` WHERE `name`='PS_OS_CHEQUE' LIMIT 1); +UPDATE `PREFIX_order_state` SET `module_name` = 'cheque' WHERE `id_order_state` = (SELECT `value` FROM `PREFIX_configuration` WHERE `name` = 'PS_OS_CHEQUE' LIMIT 1); -UPDATE `PREFIX_order_state` SET `module_name`='bankwire' WHERE `id_order_state`=(SELECT `value` FROM `PREFIX_configuration` WHERE `name`='PS_OS_BANKWIRE' LIMIT 1); +UPDATE `PREFIX_order_state` SET `module_name` = 'bankwire' WHERE `id_order_state` = (SELECT `value` FROM `PREFIX_configuration` WHERE `name` = 'PS_OS_BANKWIRE' LIMIT 1); -ALTER TABLE `PREFIX_product_shop` ADD `uploadable_files` TINYINT NOT NULL DEFAULT 0 AFTER `customizable`; \ No newline at end of file +ALTER TABLE `PREFIX_product_shop` ADD `uploadable_files` TINYINT NOT NULL DEFAULT 0 AFTER `customizable`;