在上一节中讲到了如何实现在一个安装包中安装多个扩展的理论知识。本节将依据这个理论,在j25上实现一个安装包并且进行测试。
在 上一节中提到 使用component.install.php这个文件是没有作用的。在j25或者以后的版本中可以使 用<scriptfile>script.php</scriptfile>来指定安装时执行的脚本。在实战之前先简单的谈一下 script.php这个文件。
script.php文件
有时候需要在安装扩展之前做一些检测 工作,比喻说joomla的版本是否适合安装。这些工作在xml文件中是坐不了的。因此,joomla提供了一个script.php的解决方法。这个 script.php文件主要的作用就是能够使我们在扩展安装或者卸载的时候执行一定的逻辑操作。
要使用script.php,我们需要在script.php中实现一个类,这个类的名称为com_componentNameInstallerScript。然后在这个类中实现5个方法,这个5个方法的名称如下:
- preflight 在组件安装或者更新之前执行。
- install 在组件的安装的时候(组件的安装SQL文件执行完成之后)执行。如果install返回false.那么系统就会撤销之前的所有改变。
- update 在组件更新的时候(组件的更新SQL文件执行完成之后)执行。如果update返回false.那么系统会撤销之前的所有改变。
- postflight 在组件install ,update discover_update完成之后执行。在卸载的时候不会执行。
- uninstall 在组件卸载的时候执行
下一步就是看一下这些方法的参数了。
preflight 和postflight方法有两个参数 function postflight($type ,$parent).这里的$type代表具体执行的是哪一个类型(install,update ,discover_update).$parent标示执行这个操作的对象。
install update uninstall 只有1个参数$parent .代表了执行这个操作的对象。
组件的XML文件
组件的XML文件没什么好说的。重点就是注意scriptfile标签的位置。
<description> ZMAX Catpchar Server </description> <scriptfile>script.php</scriptfile> <install> <!-- run on install--> <sql>
注意他和<description>表情在同一层级。另外需记住要包含扩展的安装文件。如下:
<!-- Administration Main File Copy Section --> <files folder="admin"> <folder>controllers</folder> <folder>helpers</folder> <folder>language</folder> <folder>lib</folder> <folder>models</folder> <folder>sql</folder> <folder>tables</folder> <folder>views</folder> <folder>extensions</folder>
这个extensions中就有我要安装的扩展的安装文件。
组件的script.php文件
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Script file of captcha component
**/
class com_captchaInstallerScript
{
/**
* method to install the component
* @return void
*/
function install($parent)
{
// $parent is the class calling this method
$this->installExtension();
}
/**
* method to uninstall the component
* @return void
**/
function uninstall($parent)
{
// $parent is the calss calling this method
echo "I am uninstalled!";
}
/**
* method to update the component
* @return void
**/
function update($parent)
{
// $parent is the class calling this method
//echo "<p>".JText::sprintf('COM_CAPTCHA_UPDATE_TEXT' ,$parent->get('manifest')->version).'</p>';
echo "I am update!";
}
/**
* method to runbefore on install/update/unistall method
* @return void
**/
function preflight($type ,$parent)
{
// $parent is the class calling this method
// $type is the type change (install ,update or discover_install)
echo "I am preflight!";
}
/**
* method to run after on install/update/uninstall method
* @return void
*/
function postflight($type ,$parent)
{
//$parent is the class calling this method
//$type is the type of change (install ,update or discover_install)
echo "I am postflight!";
}
protected function installExtension()
{
jimport('joomla.installer.helper');
$installer = new JInstaller();
$pk_path = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_captcha'.DS.'extensions'.DS;
$pk_file = "zmcaptchav1.0.0-2014-09-12.zip";
$pk_name = "ZMAX Zmcaptcha Plugin";
$package = JInstallerHelper::unpack($pk_path.$pk_file);
//print_r($package);
if($installer->install( $package['dir']))
{
$msgColor = "#E0FFE0";
$msgText = "sucessfully installed";
}
else
{
$msgColor = "#FFD0D0";
$msgText = "ERROR: install Failed!";
echo $msgText."<br/>";
}
echo $msgText."<br/>";
JInstallerHelper::cleanupInstall($pk_path.$pk_file ,$package['dir']);
}
}
?>
一定要注意 将扩展安装包的路径指定对。不然老是提示安装不成功。找到文件。
总结
上面的方法还需要修改,在实际使用中,我发现在extensions目录下还是有一些临时目录没有清理。这个可以通过 简单的修改installExtension方法来是实现。

评论 (0)