如何使用Zend Framework实现模块安装和卸载

时间:2011-01-16 11:20:45

标签: zend-framework module install uninstall

我有一个使用Zend Framework构建的web应用程序,其中包含许多模块。这些模块都是“可选的”,用于扩展功能。其中一些模块编写了自己的日志等。我正在考虑如何为这些模块实现安装和卸载。

首先,我的想法是让每个模块都有InstallationControllerUninstallController等,并让它们处理安装。但后来我开始考虑一种方法,即让每个模块包含install.iniuninstall.ini等。然后核心具有削减和行动的功能。模块uninstall.ini文件的foo示例可以是

[save_logs]
folder.remove.data.foo
folder.remove.modules.foo
file.remove.configs.foo

[complete : save_logs]
file.remove.logs.foo
db.table.truncate.foo_table1
db.table.truncate.foo_table2

然后,在运行Complete模块的卸载时,将向用户显示Save Logsfoo的选项。我可以通过这种方法看到的一个好处是一个常见的核心机制,它可以处理所有操作,以及在卸载过程中没有代码实际上是foo模块的一部分。

我之前从未在webapp上进行过这种类型的安装/卸载/更新支持,所以任何想法和提示都会很好。

2 个答案:

答案 0 :(得分:4)

我在上午的工作会议之前很快就提出了一些初步想法。你怎么看?是否应该更多地考虑和调查这种方法? 这里有一些伪代码讨论格式,它绝不是完整的函数和类集,但我认为主要的思想很清楚。

class Foo_Installer extends Zend_Module_Installer
{
    // The modules bar and exporter are needed by this module
    protected $_dependencies = array('modules' => array('bar', 'exporter'));
        // Maybe this should be expanded to include versions like below. Prehaps even be able to
        // specify a formula of a version like '>2.3 && !2.4 && !2.6' if 2.5 and 2.6 is not compatible
        // for some reason or another.
        protected $_dependencies = array('modules' => array('bar' => '1.0', 'exporter' => '2.3'));

    // Tell the installer what 'script' to use. Should be able to use sources such as xml, ini, yaml, db etc
    // The install script should contain sections for install/uninstall and update process
    protected $_installScript = 'fooInstall.xml';

    // Place to look for files for update
    protected $_repo = 'http://www.foobar.com/repo';
}


class Zend_Module_Installer
{
    protected function _checkDependencies() {
        // Check if modules in $this->_dependencies are already installed
    }

    public function install() {
        $this->_checkDependencies();

        // Parses the given source of the install script and returns installSteps
        $installSteps = $this->_getInstallSteps();

        foreach($installSteps as $installStep) {
            $installStep->perform();
        }
    }

    public function uninstall() {

    }

    public function update() {
        // Connect to repo and check for a newer version of the module.
        // I think that prehaps a standard should be that modules are distributed
        // as zip-archives so only one file needs to be downloaded. On a update server
        // a file named after a standard 'ie packages' could be present that could be
        // read to determine what packages and versions of these exist on the server
        // and if there is a new version avalible to download.
        //
        // If so we download, unzip, check dependencies, check if dependencies we don't
        // already have installet are avalible at the server, download these and itterate
        // until no more downloads are necessery. Then we start runnin the Update()
        // functions of the downloaded modules in the correct order.
    }

    protected function getInstallSteps() {
        // parses the installscript and instanciates Zend_Installer_Step objects
    }
}


// Base class for steps during installation
// This apporach makes it easy to extend the installer to be able to do specific things
// in specific enviroments. Prehaps configure a some external hardware or whatever.
class Zend_Installer_Step
{
    public function perform();
}


// Functions to handle the actual logic of creating and deleting stuff.
// Some could be standard and some could be application specific
class Zend_Installer_Step_Create_File extends Zend_Installer_Step
{
}

class Zend_Installer_Step_Delete_File extends Zend_Installer_Step
{
}

class Zend_Installer_Step_Create_Folder extends Zend_Installer_Step
{
}

class Zend_Installer_Step_Create_Db extends Zend_Installer_Step
{
}

class Zend_Installer_Step_Create_Db_Table extends Zend_Installer_Step
{
}

class Zend_Installer_Step_Create_Db_StoredProcedure extends Zend_Installer_Step
{
}

答案 1 :(得分:3)

我也将面临这一挑战,所以我们可以互相帮助。我只是想把你的一些想法添加到你已经开始概述的内容中。

我认为安装/卸载控制器会有点过分。太多的冗余代码。

安装程序核心模块如何处理软件的所有安装和卸载操作。然后,该模块将查找install.ini并卸载ini文件,并相应地执行必要的操作。如果缺少install.ini中的指令,模块也会执行默认操作。这样您就可以确保只需要将非默认行为放入ini文件中。