[-] MO : fixed the possibility to override/remove an override twice in the same session without redeclaration #PSCFV-10168

This commit is contained in:
Damien Metzger committed 2013-08-21 14:32:18 +02:00
1 parent f610b0d844
commit cf35d3762b
1 file changed
+28 -20
+28 -20
View File
@@ -1946,31 +1946,25 @@ abstract class ModuleCore
$path = Autoload::getInstance()->getClassPath($classname.'Core'); $path = Autoload::getInstance()->getClassPath($classname.'Core');
// Check if there is already an override file, if not, we just need to copy the file // Check if there is already an override file, if not, we just need to copy the file
if (!($classpath = Autoload::getInstance()->getClassPath($classname))) if (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);
// Re-generate the class index
Autoload::getInstance()->generateIndex();
return true;
}
// Check if override file is writable // Check if override file is writable
$override_path = _PS_ROOT_DIR_.'/'.Autoload::getInstance()->getClassPath($classname); $override_path = _PS_ROOT_DIR_.'/'.Autoload::getInstance()->getClassPath($classname);
if ((!file_exists($override_path) && !is_writable(dirname($override_path))) || (file_exists($override_path) && !is_writable($override_path))) if ((!file_exists($override_path) && !is_writable(dirname($override_path))) || (file_exists($override_path) && !is_writable($override_path)))
throw new Exception(sprintf(Tools::displayError('file (%s) not writable'), $override_path)); throw new Exception(sprintf(Tools::displayError('file (%s) not writable'), $override_path));
// Get a uniq id for the class, because you can override a class (or remove the override) twice in the same session and we need to avoid redeclaration
do $uniq = uniqid();
while (class_exists($classname.'OverrideOriginal_remove', false));
// Make a reflection of the override class and the module override class // Make a reflection of the override class and the module override class
$override_file = file($override_path); $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))); 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'.$uniq), implode('', $override_file)));
$override_class = new ReflectionClass($classname.'OverrideOriginal'); $override_class = new ReflectionClass($classname.'OverrideOriginal'.$uniq);
$module_file = file($this->getLocalPath().'override'.DIRECTORY_SEPARATOR.$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))); 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'.$uniq), implode('', $module_file)));
$module_class = new ReflectionClass($classname.'Override'); $module_class = new ReflectionClass($classname.'Override'.$uniq);
// Check if none of the methods already exists in the override class // Check if none of the methods already exists in the override class
foreach ($module_class->getMethods() as $method) foreach ($module_class->getMethods() as $method)
@@ -1987,7 +1981,17 @@ abstract class ModuleCore
array_splice($override_file, $override_class->getEndLine() - 1, 0, $copy_from); array_splice($override_file, $override_class->getEndLine() - 1, 0, $copy_from);
$code = implode('', $override_file); $code = implode('', $override_file);
file_put_contents($override_path, $code); file_put_contents($override_path, $code);
}
else
{
$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);
// Re-generate the class index
Autoload::getInstance()->generateIndex();
}
return true; return true;
} }
@@ -2009,14 +2013,18 @@ abstract class ModuleCore
if (!is_writable($override_path)) if (!is_writable($override_path))
return false; return false;
// Get a uniq id for the class, because you can override a class (or remove the override) twice in the same session and we need to avoid redeclaration
do $uniq = uniqid();
while (class_exists($classname.'OverrideOriginal_remove', false));
// Make a reflection of the override class and the module override class // Make a reflection of the override class and the module override class
$override_file = file($override_path); $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_remove'), implode('', $override_file))); 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_remove'.$uniq), implode('', $override_file)));
$override_class = new ReflectionClass($classname.'OverrideOriginal_remove'); $override_class = new ReflectionClass($classname.'OverrideOriginal_remove'.$uniq);
$module_file = file($this->getLocalPath().'override/'.$path); $module_file = file($this->getLocalPath().'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.'Override_remove'), implode('', $module_file))); 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_remove'.$uniq), implode('', $module_file)));
$module_class = new ReflectionClass($classname.'Override_remove'); $module_class = new ReflectionClass($classname.'Override_remove'.$uniq);
// Remove methods from override file // Remove methods from override file
$override_file = file($override_path); $override_file = file($override_path);