Authored by whb

安装配置

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Zip
  5 + * Date: 15/4/5
  6 + * Time: 下午4:54
  7 + */
  8 +
  9 +namespace QSns\Composer;
  10 +
  11 +
  12 +class Common
  13 +{
  14 + const DS = DIRECTORY_SEPARATOR;
  15 +
  16 + /**
  17 + * 安装App
  18 + * @param $appName
  19 + * @throws \Exception
  20 + */
  21 + static public function installApp($appName)
  22 + {
  23 + $appPath = dirname(dirname(__DIR__)) . self::DS . "Application" . self::DS . $appName;
  24 + if (!file_exists($appPath)) {
  25 + return;
  26 + }
  27 + $toAppPath = realpath("../../") . self::DS . 'application' . self::DS . 'modules' . self::DS . $appName;
  28 + self::moveName($appPath, $toAppPath);
  29 + }
  30 +
  31 + /**
  32 + * 卸载App
  33 + * @param $appName
  34 + */
  35 + static public function uninstallApp($appName)
  36 + {
  37 + $toAppPath = realpath("../../") . self::DS . 'application' . self::DS . 'modules' . self::DS . $appName;
  38 + self::delTree($toAppPath);
  39 + }
  40 +
  41 + /**
  42 + * 移动文件夹
  43 + * @param $appPath
  44 + * @param $toAppPath
  45 + * @throws \Exception
  46 + */
  47 + static public function moveName($appPath, $toAppPath)
  48 + {
  49 + $appRootPath = dirname($toAppPath);
  50 + if (!file_exists($appRootPath)) {
  51 + if (!mkdir($appRootPath, 0777, true)) {
  52 + throw new \Exception('Error : ' . $toAppPath . " Failed to create folders...");
  53 + }
  54 + }
  55 + if (is_writable(dirname($toAppPath)) == false) {
  56 + throw new \Exception('Error : ' . $toAppPath . " is readonly.");
  57 + }
  58 + if (file_exists($toAppPath) == true) {
  59 + self::delTree($toAppPath);
  60 + }
  61 + rename($appPath, $toAppPath);
  62 + }
  63 +
  64 + /**
  65 + * 删除目录树
  66 + * @param $dir
  67 + * @return bool
  68 + */
  69 + public static function delTree($dir)
  70 + {
  71 + $files = array_diff(scandir($dir), array('.', '..'));
  72 + foreach ($files as $file) {
  73 + (is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
  74 + }
  75 + return rmdir($dir);
  76 + }
  77 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Zip
  5 + * Date: 15/4/5
  6 + * Time: 下午2:46
  7 + */
  8 +
  9 +namespace QSns\Composer;
  10 +
  11 +use QSns\Config;
  12 +
  13 +class Install extends Common
  14 +{
  15 + static public function postInit()
  16 + {
  17 + foreach (Config::$appName as $appName) {
  18 + self::installApp($appName);
  19 + }
  20 + }
  21 +
  22 + static public function postUpdate()
  23 + {
  24 +
  25 + }
  26 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Zip
  5 + * Date: 15/4/5
  6 + * Time: 下午4:47
  7 + */
  8 +
  9 +namespace QSns\Composer;
  10 +
  11 +use QSns\Config;
  12 +class Uninstall extends Common
  13 +{
  14 + static public function prePackage()
  15 + {
  16 + foreach (Config::$appName as $appName) {
  17 + $toAppPath = realpath("../../") . self::DS . 'application' . self::DS . 'modules' . self::DS . $appName;
  18 + self::delTree($toAppPath);
  19 + }
  20 + }
  21 +
  22 + static public function postPackage()
  23 + {
  24 +
  25 + }
  26 +}