Authored by whb

安装配置

<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/4/5
* Time: 下午4:54
*/
namespace QSns\Composer;
class Common
{
const DS = DIRECTORY_SEPARATOR;
/**
* 安装App
* @param $appName
* @throws \Exception
*/
static public function installApp($appName)
{
$appPath = dirname(dirname(__DIR__)) . self::DS . "Application" . self::DS . $appName;
if (!file_exists($appPath)) {
return;
}
$toAppPath = realpath("../../") . self::DS . 'application' . self::DS . 'modules' . self::DS . $appName;
self::moveName($appPath, $toAppPath);
}
/**
* 卸载App
* @param $appName
*/
static public function uninstallApp($appName)
{
$toAppPath = realpath("../../") . self::DS . 'application' . self::DS . 'modules' . self::DS . $appName;
self::delTree($toAppPath);
}
/**
* 移动文件夹
* @param $appPath
* @param $toAppPath
* @throws \Exception
*/
static public function moveName($appPath, $toAppPath)
{
$appRootPath = dirname($toAppPath);
if (!file_exists($appRootPath)) {
if (!mkdir($appRootPath, 0777, true)) {
throw new \Exception('Error : ' . $toAppPath . " Failed to create folders...");
}
}
if (is_writable(dirname($toAppPath)) == false) {
throw new \Exception('Error : ' . $toAppPath . " is readonly.");
}
if (file_exists($toAppPath) == true) {
self::delTree($toAppPath);
}
rename($appPath, $toAppPath);
}
/**
* 删除目录树
* @param $dir
* @return bool
*/
public static function delTree($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/4/5
* Time: 下午2:46
*/
namespace QSns\Composer;
use QSns\Config;
class Install extends Common
{
static public function postInit()
{
foreach (Config::$appName as $appName) {
self::installApp($appName);
}
}
static public function postUpdate()
{
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/4/5
* Time: 下午4:47
*/
namespace QSns\Composer;
use QSns\Config;
class Uninstall extends Common
{
static public function prePackage()
{
foreach (Config::$appName as $appName) {
$toAppPath = realpath("../../") . self::DS . 'application' . self::DS . 'modules' . self::DS . $appName;
self::delTree($toAppPath);
}
}
static public function postPackage()
{
}
}
\ No newline at end of file
... ...