RefreshCacheController.class.php 2.41 KB
<?php
/**
 * 系统设置-更新缓存
 * 鲜彤 2016年08月02日18:10:25
 *
 * @update 2016-10-12 zhuxun37 修改调用方法;
 * @update 2017-07-18 Deepseath 更改请求应用更新缓存的方式,旧的方法改名为 _Index()
 */

namespace Apicp\Controller\SysSetting;

use Com\Rpc;
use VcySDK\Service;
use VcySDK\EnterprisePlugin;

class RefreshCacheController extends AbstractController
{

    public function Index()
    {
        // 按应用目录来获取
        $handle = opendir(CODE_ROOT);
        if (!$handle) {
            return false;
        }

        while (false !== ($file = readdir($handle))) {
            if ($file == '.' || $file == '..' || !is_dir(CODE_ROOT . DIRECTORY_SEPARATOR . $file)) {
                // 忽略 “.”、“..”以及文件
                continue;
            }

            // 构造应用对应的缓存清理文件
            $clearCacheRpcFile = CODE_ROOT;
            $clearCacheRpcFile .= DIRECTORY_SEPARATOR . $file;
            $clearCacheRpcFile .= DIRECTORY_SEPARATOR . 'Rpc';
            $clearCacheRpcFile .= DIRECTORY_SEPARATOR . 'Controller';
            $clearCacheRpcFile .= DIRECTORY_SEPARATOR . 'Cache';
            $clearCacheRpcFile .= DIRECTORY_SEPARATOR . 'ClearController.class.php';
            if (!is_file($clearCacheRpcFile)) {
                // 如果执行文件不存在,则忽略
                continue;
            }
            
            $url = rpcUrl("/{$file}/Rpc/Cache/Clear");
            // 请求对应 RPC
            $rpc = Rpc::phprpc($url);
            call_user_func(array($rpc, 'Index'));
        }
        closedir($handle);

        return true;
    }

    /**
     * 旧的(HR)缓存更新方式
     * 备份
     */
    protected function _Index()
    {

        // 获取安装的应用列表
        $pluginSDK = new EnterprisePlugin(Service::instance());
        $pluginList = $pluginSDK->listAll();
        // RPC通知应用清除缓存
        foreach ($pluginList as $_plugin) {
            // 已安装的才清缓存
            if ($pluginSDK->isInstall($_plugin['allowStatus'])) {
                continue;
            }

            $identifier = ucfirst($_plugin['plIdentifier']);
            $rpc = Rpc::phprpc($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . QY_DOMAIN . '/' . $identifier . '/' . 'Rpc/Cache/Clear');
            call_user_func(array($rpc, 'Index'));
        }

        return true;
    }

}