提交 c6eba508 编写于 作者: T Terry

extension generate service

上级 61968d79
...@@ -15,6 +15,9 @@ return [ ...@@ -15,6 +15,9 @@ return [
'remoteService' => [ 'remoteService' => [
'class' => 'fecshop\services\extension\RemoteService', 'class' => 'fecshop\services\extension\RemoteService',
], ],
'generate' => [
'class' => 'fecshop\services\extension\Generate',
],
], ],
], ],
]; ];
\ No newline at end of file
<?php
/*
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services\extension;
//use fecshop\models\mysqldb\cms\StaticBlock;
use Yii;
use fecshop\services\Service;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Generate extends Service
{
/**
* @param $param | array
* 数组子项:package, addon_folder, namespaces, addon_name, addon_author
* 创建应用初始化包到指定的文件路径。
*/
public function createAddonsFiles($param)
{
$package = isset($param['package']) ? $param['package'] : '';
$addon_folder = isset($param['addon_folder']) ? $param['addon_folder'] : '';
$namespaces = isset($param['namespaces']) ? $param['namespaces'] : '';
$addon_name = isset($param['addon_name']) ? $param['addon_name'] : '';
$addon_author = isset($param['addon_author']) ? $param['addon_author'] : '';
if (!$package || !$addon_folder || !$namespaces || !$addon_name || !$addon_author) {
return false;
}
// 创建文件夹
if (!$this->createFolder($param)) {
return false;
}
// 开始渲染gii 模板
//得到应用文件夹
$addonPath = Yii::getAlias('@addons/'.$package.'/'.$addon_folder);
// config.php文件写入
$viewFile = '@fecshop/services/extension/generate/config.php';
$configContent =Yii::$app->view->renderFile($viewFile, $param);
// 写入的文件路径
$addonConfigFile = $addonPath. '/config.php';
if (@file_put_contents($addonConfigFile, $configContent) === false) {
Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile);
return false;
}
// 写入Install
$viewFile = '@fecshop/services/extension/generate/administer/Install.php';
$configContent =Yii::$app->view->renderFile($viewFile, $param);
// 写入的文件路径
$addonConfigFile = $addonPath. '/administer/Install.php';
if (@file_put_contents($addonConfigFile, $configContent) === false) {
Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile);
return false;
}
// 写入Upgrade
$viewFile = '@fecshop/services/extension/generate/administer/Upgrade.php';
$configContent =Yii::$app->view->renderFile($viewFile, $param);
// 写入的文件路径
$addonConfigFile = $addonPath. '/administer/Upgrade.php';
if (@file_put_contents($addonConfigFile, $configContent) === false) {
Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile);
return false;
}
// 写入 Uninstall
$viewFile = '@fecshop/services/extension/generate/administer/Uninstall.php';
$configContent =Yii::$app->view->renderFile($viewFile, $param);
// 写入的文件路径
$addonConfigFile = $addonPath. '/administer/Uninstall.php';
if (@file_put_contents($addonConfigFile, $configContent) === false) {
Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile);
return false;
}
return true;
}
public function createFolder($param)
{
$package = isset($param['package']) ? $param['package'] : '';
$addon_folder = isset($param['addon_folder']) ? $param['addon_folder'] : '';
$namespaces = isset($param['namespaces']) ? $param['namespaces'] : '';
$addon_name = isset($param['addon_name']) ? $param['addon_name'] : '';
$addon_author = isset($param['addon_author']) ? $param['addon_author'] : '';
$addonPath = Yii::getAlias('@addons/'.$package.'/'.$addon_folder);
// 创建文件夹
if (!$this->createDir($addonPath)) {
return false;
}
// administer
$administerPath = $addonPath . '/administer';
if (!$this->createDir($administerPath)) {
return false;
}
// app/appfront
$appfrontPath = $addonPath . '/app/appfront';
if (!$this->createDir($appfrontPath)) {
return false;
}
// models
$modelPath = $addonPath . '/models';
if (!$this->createDir($modelPath)) {
return false;
}
// services
$servicesPath = $addonPath . '/services';
if (!$this->createDir($servicesPath)) {
return false;
}
return true;
}
public function createDir($dir)
{
if (is_dir($dir)) {
Yii::$service->helper->errors->add('dir['.$dir.'] is exist');
return false;
}
return mkdir($dir,0777,true);
}
}
<?php
/**
* 应用卸载类生成模板
*/
echo "<?php\n";
?>
/**
* Fecmall Addons
*/
namespace <?= $namespaces ?>\administer;
use Yii;
/**
* 应用安装类
* 您可以在这里添加类变量,在配置中的值可以注入进来。
*/
class Install implements \fecshop\services\extension\InstallInterface
{
// 安装初始版本号,不需要改动,不能为空
public $version = '1.0.0';
// 类变量,在config.php中可以通过配置注入值
public $test;
/**
* @return mixed|void
*/
public function run()
{
/**
* 小知识:事务操作中,表数据的操作是可以回滚的,表结构改变的sql是无法回滚的。
$sql = "
DROP TABLE IF EXISTS `fecmall_addon_test1`;
CREATE TABLE `fecmall_addon_test1` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`merchant_id` int(10) unsigned DEFAULT '0' COMMENT '商户id',
`title` varchar(50) NOT NULL COMMENT '标题',
`cover` varchar(100) DEFAULT '' COMMENT '封面',
`seo_key` varchar(50) DEFAULT '' COMMENT 'seo关键字',
`seo_content` varchar(1000) DEFAULT '' COMMENT 'seo内容',
`cate_id` int(10) DEFAULT '0' COMMENT '分类id',
`description` char(140) DEFAULT '' COMMENT '描述".$this->test." ',
`position` smallint(5) NOT NULL DEFAULT '0' COMMENT '推荐位',
`content` longtext COMMENT '文章内容',
`link` varchar(100) DEFAULT '' COMMENT '外链',
`author` varchar(40) DEFAULT '' COMMENT '作者',
`view` int(10) NOT NULL DEFAULT '0' COMMENT '浏览量',
`sort` int(10) NOT NULL DEFAULT '0' COMMENT '优先级',
`status` tinyint(4) DEFAULT '1' COMMENT '状态',
`created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `article_id` (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='扩展_文章表';
-- ----------------------------
-- Table structure for rf_addon_article_adv
-- ----------------------------
DROP TABLE IF EXISTS `fecmall_addon_test2`;
CREATE TABLE `fecmall_addon_test2` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
`merchant_id` int(10) unsigned DEFAULT '0' COMMENT '商户id',
`title` varchar(30) NOT NULL DEFAULT '' COMMENT '标题',
`cover` varchar(100) DEFAULT '' COMMENT '图片',
`location_id` int(11) DEFAULT '0' COMMENT '广告位ID',
`silder_text` varchar(150) DEFAULT '' COMMENT '图片描述',
`start_time` int(10) DEFAULT '0' COMMENT '开始时间',
`end_time` int(10) DEFAULT '0' COMMENT '结束时间',
`jump_link` varchar(150) DEFAULT '' COMMENT '跳转链接',
`jump_type` tinyint(4) DEFAULT '1' COMMENT '跳转方式[1:新标签; 2:当前页]',
`sort` int(10) DEFAULT '0' COMMENT '优先级',
`status` tinyint(4) DEFAULT '1' COMMENT '状态',
`created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='扩展_文章_幻灯片表';
";
// 执行sql, 创建表结构的时候,这个函数会返回0,因此不能以返回值作为return
Yii::$app->getDb()->createCommand($sql)->execute();
*/
return true;
}
}
\ No newline at end of file
<?php
/**
* 应用卸载类生成模板
*/
echo "<?php\n";
?>
/**
* Fecmall Addons
*/
namespace <?= $namespaces ?>\administer;
use Yii;
/**
* 应用安装类
* 您可以在这里添加类变量,在配置中的值可以注入进来。
*/
class Uninstall implements \fecshop\services\extension\UninstallInterface
{
/**
* 应用执行卸载的步骤执行的函数。
*/
public function run()
{
/*
$sql = "
// DROP TABLE IF EXISTS `fecmall_addon_test1`;
// DROP TABLE IF EXISTS `fecmall_addon_test2`;
";
// 执行sql, 创建表结构的时候,这个函数会返回0,因此不能以返回值作为return
Yii::$app->getDb()->createCommand($sql)->execute();
*/
return true;
}
}
\ No newline at end of file
<?php
/**
* 应用卸载类生成模板
*/
echo "<?php\n";
?>
/**
* Fecmall Addons
*/
namespace <?= $namespaces ?>\administer;
use Yii;
/**
* 应用安装类
* 您可以在这里添加类变量,在配置中的值可以注入进来。
*/
class Upgrade implements \fecshop\services\extension\UpgradeInterface
{
/**
* @var array
* 必须按照版本号依次填写,否则升级会导致问题。
* 如果安装的初始版本号为1.0.0,那么下面的升级版本号必须比初始版本号大才行
*/
public $versions = [
// '1.0.1',
// '1.0.2',
// '1.0.3',
];
/**
* @param $version 最新版本号
* @return boolean
* 升级执行的函数,您可以在各个版本号需要执行的部分写入相应的操作。
*/
public function run($version)
{
/**
* 下面仅仅是一个sql例子,您可以将其换成您自己要升级的内容
switch ($version)
{
case '1.0.1' :
// 增加测试 - 冗余的字段
$sql = "ALTER TABLE fecmall_addon_test1 ADD COLUMN redundancy_field_5255 varchar(48);";
Yii::$app->getDb()->createCommand($sql)->execute();
break;
case '1.0.2' :
// 删除测试 - 冗余的字段
$sql = "ALTER TABLE fecmall_addon_test1 ADD COLUMN redundancy_field_566 varchar(48);";
Yii::$app->getDb()->createCommand($sql)->execute();
break;
case '1.0.3' :
// 删除测试 - 冗余的字段
$sql = "ALTER TABLE fecmall_addon_test1 ADD COLUMN redundancy_field_567 varchar(48);";
Yii::$app->getDb()->createCommand($sql)->execute();
break;
}
*/
return true;
}
}
\ No newline at end of file
<?php
/**
* 应用卸载类生成模板
*/
echo "<?php\n";
?>
/**
* Fecmall Addons Config File
*/
// set namespace alisa
Yii::setAlias('@<?= $namespaces ?>', dirname(dirname(dirname(__DIR__))).'/addons/<?= $package ?>/<?= $addon_folder ?>/');
return [
// 插件信息
'info' => [
'name' => '<?= $addon_name ?>',
'author' => '<?= $addon_author ?>',
],
// 插件管理部分
'administer' => [
'install' => [
'class' => '<?= $namespaces ?>\administer\Install',
// 其他引入的属性,类似yii2组件的方式写入即可
'test' => 'test_data',
],
'upgrade' => [
'class' => '<?= $namespaces ?>\administer\Upgrade',
],
'uninstall' => [
'class' => '<?= $namespaces ?>\administer\Uninstall',
],
],
// 各个入口的配置
'app' => [
// 公共层部分配置
'common' => [
'enable' => true,
// 公用层的具体配置下载下面
'config' => [
'services' => [
//'cart' => [
// 'class' => 'fecshop\rediscart\services\Cart',
// 'childService' => [
// 'quote' => [
// 'class' => 'fecshop\rediscart\services\cart\Quote',
// ],
// 'quoteItem' => [
// 'class' => 'fecshop\rediscart\services\cart\QuoteItem',
// ],
// ]
//]
],
]
],
// 1.appfront层
'appfront' => [
// appfront入口的开关,如果false,则会失效
'enable' => true,
'config' => [
// yii class rewrite map
'yiiClassMap' => [
// 'fecshop\app\appfront\helper\test\My' => '@appfront/helper/My.php',
],
// 重写model和block
'fecRewriteMap' => [
// '\fecshop\app\appfront\modules\Cms\block\home\Index' => '\fectfurnilife\app\appfront\modules\Cms\block\home\Index',
// '\fecshop\app\appfront\modules\Customer\block\address\Edit' => '\fectfurnilife\app\appfront\modules\Customer\block\address\Edit',
],
'modules' => [
//'checkout' => [
// 'controllerMap' => [
// 'cartinfo' => 'fectfurnilife\app\appfront\modules\Checkout\controllers\CartInfoController',
// ],
//],
],
],
],
// html5入口
'apphtml5' =>[],
// appserver入口(vue 微信小程序等api)
'appserver' =>[],
// appapi入口,和第三方交互的api
'appapi' =>[],
// 后台部分
'appadmin' =>[],
// console,命令行脚本端
'console' =>[],
],
];
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册