2017-01-05 99 views
2

覆蓋文件夾功能和衝突?覆蓋文件夾功能和衝突?

我在prestashop基本代碼中有一個名爲group.php的類文件。 我想添加一個新字段,並對該group.php文件進行一些功能更改。

我已經創建了一個自定義模塊,並做了那些改變。當我安裝該模塊時,我繼承的group.php文件被髮送到Base Override文件夾並保持在那裏。

現在的問題是,

該如何覆蓋功能在工作?

衝突是如何由prestashop管理的?

例如:我有2個模塊,分別覆蓋相同的文件group.php。 如果我同時安裝那些group.php將在基本覆蓋文件夾中的2模塊?

+0

您好Kirubanidhi,你需要關於這個問題的更多信息?如果我正確回答了您的疑慮,您是否介意接受我關於此主題的答案? –

回答

3

下面是來自ModuleaddOverride()方法:

/** 
* Add all methods in a module override to the override class 
* 
* @param string $classname 
* @return bool 
*/ 
public function addOverride($classname) 
{ 
    $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); 
     // Re-generate the class index 
     Autoload::getInstance()->generateIndex(); 
     return true; 
    } 

    // Check if override file is writable 
    $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))) 
     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'.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->getName())) 
      throw new Exception(sprintf(Tools::displayError('The method %1$s in the class %2$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 %1$s in the class %2$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); 
    array_splice($override_file, $override_class->getEndLine() - 1, 0, $copy_from); 
    $code = implode('', $override_file); 
    file_put_contents($override_path, $code); 

    return true; 
} 

正如評論指出衝突的是手柄的方法和屬性擴展。

如果您嘗試安裝覆蓋新方法或新屬性的模塊,則一切正常,兩個文件將合併。

如果您嘗試安裝覆蓋已被其他模塊覆蓋的方法的模塊,則安裝將失敗。

2

當兩個模塊覆蓋同一個文件時,PrestaShop會在被覆蓋的函數的頂部添加一個註釋來標識哪個模塊覆蓋了哪個函數。該意見的

例子是:

/* 
* module: supercheckout 
* date: 2016-12-01 08:03:28 
* version: 1.0.1 
*/ 
public function overriddenFunction() 
{ 
    parent::mylogout(); 
    -- Custom Code Here -- 
} 

你可以在你在你的情況下結束(/override/classes/group.php)重寫的文件進行檢查。

只有當任何模塊被卸載並且如果只有一個模塊覆蓋模塊,然後該文件被刪除,重寫的代碼纔會使用這些註釋刪除。