如何重写fecshop services product ProductMongodb

技术分享 · Fecmall · 于 5年前 发布 · 1503 次阅读

fecshop 的services重写,可以通过配置进行重写,但是对于product这种可以多种实现的如何重写呢?

思路原理解析

1.在文件:https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php#L74

可以看到代码:

$currentService = $this->getStorageService($this);

对于 getStorageService($this)方法就是:https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php#L223

public function getStorageService($object)
    {
        $className = get_class($object);
        if (!isset($object->storage) || !$object->storage) {
            throw new InvalidConfigException('you must config class var $storage in '.$className);
            
            return false;
        }
        if ($object->storagePath) {
            $storagePath = '\\'.trim($object->storagePath, '\\').'\\';
        } else {
            $storagePath = '\\'.strtolower($className).'\\';
        }
        $storageServiceClass =  $storagePath.ucfirst($object->storage);
    
        if (!class_exists($storageServiceClass)) {
            throw new InvalidCallException('class ['.$storageServiceClass.'] is not exist , you must add the class before you use it');
            
            return false;
        }
        
        return $storageServiceClass;
    }

通过上面的代码可以看到,通过类变量storagePathstorage 拼接得到了ProductMongodb的类 (注意,storagePath是namespace地址,而不是文件路径)

上面是原理,因此,如果你想重写ProductMongo, 可以用这个原理来实现重写

例子

1.添加配置

@common/config/fecshop_local_services/Product 配置文件中添加

<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 *
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
return [
    'product' => [
        'storagePath' => 'common\\local\\local_services\\product',
        'storage' => 'ProductMongodb',
        // 属性组的配置
        'customAttrGroup' => [

也就是在原来的配置内容的基础上添加配置

'storagePath' => 'common\\local\\local_services\\product',
'storage' => 'ProductMongodb',

2.新建文件 common\local\local_services\product\ProductMongodb.php

让这个类继承 fecshop\services\product\ProductMongodb ,然后重写里面的方法即可

共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics