2017-01-05 6 views
2

フォルダの機能と競合を上書きしますか?フォルダの機能と競合を上書きしますか?

私はgroup.phpと呼ばれるクラスファイルをprestashopベースコードに持っています。 新しいフィールドを追加して、group.phpファイルにいくつかの機能変更を加えたいとします。

私はカスタムモジュールを作成し、そこに変更を加えました。そのモジュールをインストールすると、継承されたgroup.phpファイルがベースオーバーライドフォルダに送られ、そこにとどまります。

は今、私の質問はどのようにオーバーライド機能が働いている

、ありますか?

事前衝突によって競合がどのように管理されますか。

例えば、私は同じファイルを別々にオーバーライドする2つのモジュールを持っていますgroup.php。 2台のモジュールを同時にインストールすると、group.phpがベースオーバーライドフォルダにありますか?ここ

+0

こんにちはキルバニディ、この件に関する情報は必要ですか?私があなたの懸念に正しく答えたら、このスレッドを閉じるために私の答えを受け入れる気にならないでしょうか? –

答えて

3

ModuleクラスからaddOverride()方法であって、コメント競合に述べたように

/** 
* 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つのファイルが結合されます。

すでに別のモジュールによって上書きされたメソッドをオーバーライドするモジュールをインストールしようとすると、インストールは失敗します。

2

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)でオーバーライドされたファイルでこれを確認することができます。

オーバーライドされたコードは、モジュールがアンインストールされた場合にのみこれらのコメントを使用して削除され、モジュールが1つだけモジュールをオーバーライドするとファイルが削除されます。

関連する問題