doutui9606 2014-12-01 14:05
浏览 30

使用Phalcon创建插件系统

I have been looking into how to create a plugin system with Phalcon. I have checked INVO tutorial, Events Management to be more specific. It doesn't look like this is what I exactly need.

In this example it is loading the plugins manually and before establishing database connection.

  1. I need to be able to access the database to check if the plugin actually installed and activated. So I can retrieve settings of installed & activated plugins as well as dynamically add them to the app.

  2. I need to be able to attach the plugins pretty much anywhere; controllers (before / after / within method execution), models (before, after, within method execution) etc.

Does Phalcon provide such feature or do I have to ahead and try to create my own logic without using any framework feature?

  • 写回答

1条回答 默认 最新

  • duanlu1959 2014-12-02 14:39
    关注

    Do you have any examples of what you would have plugins do? Or specifics about where Events Management is lacking?

    I think you should be able able to get a very flexible system using dependency injection and events management.

    It appears you can use the phalcon models before running the application, as long as you place the code after setting the db in the injector.

    $plugins = \Plugin::find([
        'active = :active:',
        'bind'=>[
            'active'=>1
        ]
    ]);
    
    foreach($plugins as $plugin){
        if(file_exists($plugin->filename)){
           include $plugin->filename;
        }
    }
    

    and in the file you could have code to subscribe to events, and/or add new dependencies.

    // plugin file - for a db logger
    $eventsManager = new \Phalcon\Events\Manager();
    $logger = new \PhalconX\Logger\Adapter\Basic();
    $profiler = $phalconDi->getProfiler();
    $eventsManager->attach('db', function($event, $phalconConnection) use ($logger, $profiler) {
        if ($event->getType() == 'beforeQuery') {
            $profiler->startProfile($phalconConnection->getSQLStatement());
            $logger->log($phalconConnection->getSQLStatement(), \Phalcon\Logger::INFO, $phalconConnection->getSQLVariables());
        }
        if ($event->getType() == 'afterQuery') {
            $profiler->stopProfile();
        }
    }); 
    

    or

    class myHelper{ ... }
    
    $phalconDi->set('myHelper', function() use ($phalconConfig, $phalconDi) {
        $helper = new myHelper();
        $helper->setDi( $phalconDi );
        return $helper;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算