提交 bd416342 编写于 作者: R root

add

上级 6929a85c
......@@ -11,4 +11,9 @@ return [
'modules'=>$modules,
/* only config in front web */
'bootstrap' => ['store'],
'params' => [
/* appfront base theme dir */
'appfrontBaseTheme' => '@fecshop/app/appfront/theme/base/front',
'appfrontBaseLayoutName'=> 'main.php',
],
];
<?php
namespace fecshop\app\appfront\modules;
use Yii;
use fec\helpers\CConfig;
use fec\controllers\FecController;
use yii\base\InvalidValueException;
class AppfrontController extends FecController
{
protected $_currentLayoutFile = 'main.php';
protected $_themeDir = '@fecshop/app/appfront/theme/base/default';
public function beforeAction($action){
if(parent::beforeAction($action)){
Yii::$app->page->theme->fecshopThemeDir = Yii::getAlias($this->_themeDir);
return true;
/**
* init theme component property : $fecshopThemeDir and $layoutFile
* $fecshopThemeDir is appfront base theme directory.
* layoutFile is current layout relative path.
*/
public function init(){
if(!Yii::$app->page->theme->fecshopThemeDir){
Yii::$app->page->theme->fecshopThemeDir = Yii::getAlias(CConfig::param('appfrontBaseTheme'));
}
if(!Yii::$app->page->theme->layoutFile){
Yii::$app->page->theme->layoutFile = CConfig::param('appfrontBaseLayoutName');
}
}
public function render($view, $params = []){
$viewFile = '';
$relativeFile = $this->module->id.'/'.$this->id.'/'.$view.'.php';
$absoluteDir = Yii::$app->page->theme->getThemeDirArr();
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.'/'.$relativeFile;
if(file_exists($file)){
$viewFile = $file;
break;
}
}
}
if(!$viewFile){
$notExistFile = [];
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.'/'.$relativeFile;
$notExistFile[] = $file;
}
}
throw new InvalidValueException('view file is not exist in'.implode(',',$notExistFile));
}
$content = $this->getView()->renderFile($viewFile, $params, $this);
/**
* @property $view|String , (only) view file name ,by this module id, this controller id , generate view relative path.
* @property $params|Array,
* 1.get exist view file from mutil theme by theme protity.
* 2.get content by yii view compontent function renderFile() ,
*/
public function render($view, $params = []){
$viewFile = Yii::$app->page->theme->getViewFile($view);
$content = Yii::$app->view->renderFile($viewFile, $params, $this);
return $this->renderContent($content);
}
/**
* Get current layoutFile absolute path from mutil theme dir by protity
*/
public function findLayoutFile($view){
$layoutFile = '';
$relativeFile = '/layouts/'.$this->_currentLayoutFile;
$relativeFile = 'layouts/'.Yii::$app->page->theme->layoutFile;
$absoluteDir = Yii::$app->page->theme->getThemeDirArr();
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.$relativeFile;
$file = $dir.'/'.$relativeFile;
if(file_exists($file)){
$layoutFile = $file;
return $layoutFile;
......
<?php
namespace fecshop\app\appfront\modules\Cms\block\widget;
use Yii;
use fecshop\app\appfront\modules\AppfrontController;
class Test
{
public $terry;
# վϢ
public function getLastData()
{
return [
'i' => $this->terry,
'love' => 'loves',
'you' => 'terry',
];
}
}
<?php
namespace fecshop\app\appfront\modules\Cms\controllers;
use Yii;
use fecshop\app\appfront\modules\AppfrontController;
class HomeController extends AppfrontController
{
protected $_currentLayoutFile = 'home.php';
# վϢ
public function actionIndex()
{
//echo 111;exit;
echo Yii::$app->page->widget->render('love');
Yii::$app->page->theme->layoutFile = 'home.php';
return $this->render($this->action->id,[]);
}
}
......
<?php
/**
* FecShop file.
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appfront\theme\base\front\assets;
use yii\web\AssetBundle;
/**
* Page services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [];
public $cssOptions = [ 'position' => \yii\web\View::POS_HEAD ];
public $js = [];
public $jsOptions = [ 'position' => \yii\web\View::POS_END ]; # POS_HEAD
public $depends = [
//'fecshop\app\appfront\theme\BaseAsset',
'fecshop\app\appfront\theme\base\front\assets\AppFrontAsset',
'fecshop\app\appfront\theme\base\front\assets\IEAsset',
'fecshop\app\appfront\theme\base\front\assets\LtIE9Asset',
];
}
<?php
/**
* FecShop file.
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appfront\theme\base\front\assets;
use yii\web\AssetBundle;
/**
* Page services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class AppFrontAsset extends AssetBundle
{
public $sourcePath = '@fecshop/app/appfront/theme/base/front/assets';
public $css = [
'css/style.css',
];
public $js = [
'js/jquery-3.0.0.min.js',
];
}
<?php
/**
* FecShop file.
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appfront\theme\base\front\assets;
use yii\web\AssetBundle;
/**
* Page services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class IEAsset extends AssetBundle
{
public $sourcePath = '@fecshop/app/appfront/theme/base/front/assets';
public $cssOptions = ['condition' => 'if IE'];
public $css = [
'css/ie.css',
];
}
<?php
/**
* FecShop file.
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appfront\theme\base\front\assets;
use yii\web\AssetBundle;
/**
* Page services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class LtIE9Asset extends AssetBundle
{
public $sourcePath = '@fecshop/app/appfront/theme/base/front/assets';
public $cssOptions = ['condition' => 'lt IE 9'];
public $css = [
'css/ltie9.css',
];
/*
public $jsOptions = [
//'position' => \yii\web\View::POS_END ,
'condition' => 'lt IE 9'
];
public $js = [
//'dwz_jui-master/js/speedup.js',
//'dwz_jui-master/jquery-1.11.3.min.js',
];
public $depends = [
];
*/
}
.body{
color:#ccc;
font-size:12px;
}
\ No newline at end of file
<?php
/* @var $this \yii\web\View */
/* @var $content string */
use fecshop\app\appfront\theme\base\front\assets\AppAsset;
AppAsset::register($this);
//var_dump( $cssAndJs['js']);exit;
//$this->assetBundles["fecadmin\myassets\AppAsset"]->js = $cssAndJs['js'];
//$this->assetBundles["fecadmin\myassets\AppAsset"]->css = $cssAndJs['css'];
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php $this->head() ?>
<?php
//$publishedPath = $this->assetManager->publish('@fecadmin/myassets/dwz_jui-master/dwz.frag.xml');
?>
</head>
<body>
<?php $this->beginBody() ?>
<div id="layout">
<div id="header">
</div>
<?= $content ?>
</div>
<footer class="footer">
<div class="container">
</div>
</footer>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
......@@ -26,7 +26,32 @@ return [
'theme' => [
'class' => 'fecshop\services\page\Theme',
],
'widget' => [
'class' => 'fecshop\services\page\Widget',
# 定义默认方法,也就是widgetConfig 里面各个部件里面的method如果没有填写
# 则使用该配置。
# 'defaultObMethod' => 'getLastData',
'widgetConfig' => [
'menu' =>[
# 必填
'class' => 'fec\block\TestMenu',
# view 的绝对路径配置方式
'view' => '@fec/views/testmenu/index.php',
# 下面为选填
'method'=> 'getLastData',
'terry1'=> 'My1',
'terry2'=> 'My2',
],
'love' => [
'class' => 'fecshop\app\appfront\modules\Cms\block\widget\Test',
# 根据多模板的优先级,依次去模板找查找该文件,直到找到这个文件。
'view' => 'cms/home/test.php',
'terry' => 'II',
]
]
],
'currency' => [
'class' => 'fecshop\services\page\Currency',
'currencys' => [
......
#!/usr/bin/env php
<?php
/**
* Yii Application Initialization Tool
*
* In order to run in non-interactive mode:
*
* init --env=Development --overwrite=n
*
* @author Alexander Makarov <sam@rmcreative.ru>
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
if (!extension_loaded('openssl')) {
die('The OpenSSL PHP extension is required by Yii2.');
}
$params = getParams();
$root = str_replace('\\', '/', __DIR__);
$root = "$root/../../..";
$envs = require("$root/vendor/fancyecommerce/fecshop/environments/index.php");
$envNames = array_keys($envs);
echo "Yii Application Initialization Tool v1.0\n\n";
$envName = null;
if (empty($params['env']) || $params['env'] === '1') {
/*
echo "Which environment do you want the application to be initialized in?\n\n";
foreach ($envNames as $i => $name) {
echo " [$i] $name\n";
}
echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
$answer = trim(fgets(STDIN));
if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) {
echo "\n Quit initialization.\n";
exit(0);
}
if (isset($envNames[$answer])) {
$envName = $envNames[$answer];
}
*/
$envName = 'prod';
} else {
$envName = $params['env'];
}
if (!in_array($envName, $envNames)) {
$envsList = implode(', ', $envNames);
echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n";
exit(2);
}
$env = $envs[$envName];
if (empty($params['env'])) {
echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
$answer = trim(fgets(STDIN));
if (strncasecmp($answer, 'y', 1)) {
echo "\n Quit initialization.\n";
exit(0);
}
}
echo "\n Start initialization ...\n\n";
$files = getFileList("$root/vendor/fancyecommerce/fecshop/environments/{$env['path']}");
var_dump($files);exit;
if (isset($env['skipFiles'])) {
$skipFiles = $env['skipFiles'];
array_walk($skipFiles, function(&$value) use($env, $root) { $value = "$root/$value"; });
$files = array_diff($files, array_intersect_key($env['skipFiles'], array_filter($skipFiles, 'file_exists')));
}
$all = false;
foreach ($files as $file) {
if (!copyFile($root, "vendor/fancyecommerce/fecshop/environments/{$env['path']}/$file", $file, $all, $params)) {
break;
}
}
$callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable', 'createSymlink'];
foreach ($callbacks as $callback) {
if (!empty($env[$callback])) {
$callback($root, $env[$callback]);
}
}
echo "\n ... initialization completed.\n\n";
function getFileList($root, $basePath = '')
{
$files = [];
$handle = opendir($root);
while (($path = readdir($handle)) !== false) {
if ($path === '.git' || $path === '.svn' || $path === '.' || $path === '..') {
continue;
}
$fullPath = "$root/$path";
$relativePath = $basePath === '' ? $path : "$basePath/$path";
if (is_dir($fullPath)) {
$files = array_merge($files, getFileList($fullPath, $relativePath));
} else {
$files[] = $relativePath;
}
}
closedir($handle);
return $files;
}
function copyFile($root, $source, $target, &$all, $params)
{
if (!is_file($root . '/' . $source)) {
echo " skip $target ($source not exist)\n";
return true;
}
if (is_file($root . '/' . $target)) {
if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
echo " unchanged $target\n";
return true;
}
if ($all) {
echo " overwrite $target\n";
} else {
echo " exist $target\n";
echo " ...overwrite? [Yes|No|All|Quit] ";
$answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
if (!strncasecmp($answer, 'q', 1)) {
return false;
} else {
if (!strncasecmp($answer, 'y', 1)) {
echo " overwrite $target\n";
} else {
if (!strncasecmp($answer, 'a', 1)) {
echo " overwrite $target\n";
$all = true;
} else {
echo " skip $target\n";
return true;
}
}
}
}
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
return true;
}
echo " generate $target\n";
@mkdir(dirname($root . '/' . $target), 0777, true);
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
return true;
}
function getParams()
{
$rawParams = [];
if (isset($_SERVER['argv'])) {
$rawParams = $_SERVER['argv'];
array_shift($rawParams);
}
$params = [];
foreach ($rawParams as $param) {
if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
$name = $matches[1];
$params[$name] = isset($matches[3]) ? $matches[3] : true;
} else {
$params[] = $param;
}
}
return $params;
}
function setWritable($root, $paths)
{
foreach ($paths as $writable) {
if (is_dir("$root/$writable")) {
echo " chmod 0777 $writable\n";
@chmod("$root/$writable", 0777);
} else {
echo "\n Error. Directory $writable does not exist. \n";
}
}
}
function setExecutable($root, $paths)
{
foreach ($paths as $executable) {
echo " chmod 0755 $executable\n";
@chmod("$root/$executable", 0755);
}
}
function setCookieValidationKey($root, $paths)
{
foreach ($paths as $file) {
echo " generate cookie validation key in $file\n";
$file = $root . '/' . $file;
$length = 32;
$bytes = openssl_random_pseudo_bytes($length);
$key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
$content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
file_put_contents($file, $content);
}
}
function createSymlink($root, $links) {
foreach ($links as $link => $target) {
echo " symlink " . $root . "/" . $target . " " . $root . "/" . $link . "\n";
//first removing folders to avoid errors if the folder already exists
@rmdir($root . "/" . $link);
//next removing existing symlink in order to update the target
if (is_link($root . "/" . $link)) {
@unlink($root . "/" . $link);
}
@symlink($root . "/" . $target, $root . "/" . $link);
}
}
......@@ -23,7 +23,7 @@ class Theme extends ChildService
/**
* user current theme dir. Highest priority
*/
public $currentThemeDir;
public $localThemeDir;
/**
* $thirdThemeDir | Array
* user current theme dir.Second priority.
......@@ -34,14 +34,20 @@ class Theme extends ChildService
* fecshop theme dir. lower priority
*/
public $fecshopThemeDir ;
/**
* current layout file path.
*/
public $layoutFile;
/**
* array that contains mutil theme dir.
*/
protected $_themeDirArr;
public function getThemeDirArr(){
if(!$this->_themeDirArr){
$arr = [];
$arr[] = Yii::getAlias($this->currentThemeDir);
$arr[] = Yii::getAlias($this->localThemeDir);
$thirdThemeDirArr = $this->thirdThemeDir;
if(!empty($thirdThemeDirArr) && is_array($thirdThemeDirArr)){
foreach($thirdThemeDirArr as $theme){
......@@ -54,6 +60,43 @@ class Theme extends ChildService
return $this->_themeDirArr;
}
/**
* find theme file by mutil theme ,if not find view file and $throwError=true, it will throw InvalidValueException.
*/
public function getViewFile($view,$throwError=true){
$view = trim($view);
if(substr($view,0,1) == '@'){
return Yii::getAlias($view);
}
$relativeFile = '';
$module = Yii::$app->controller->module;
if($module && $module->id){
$relativeFile = $module->id.'/';
}
$relativeFile .= Yii::$app->controller->id.'/'.$view.'.php';
$absoluteDir = Yii::$app->page->theme->getThemeDirArr();
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.'/'.$relativeFile;
if(file_exists($file)){
return $file;
}
}
}
/* not find view file */
if($throwError){
$notExistFile = [];
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.'/'.$relativeFile;
$notExistFile[] = $file;
}
}
throw new InvalidValueException('view file is not exist in'.implode(',',$notExistFile));
}else{
return false;
}
}
......
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services\page;
use Yii;
use yii\base\InvalidValueException;
use yii\base\InvalidConfigException;
use fec\helpers\CSession;
use fec\helpers\CUrl;
use fecshop\services\ChildService;
/**
* Breadcrumbs services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Widget extends ChildService
{
public $defaultObMethod = 'getLastData';
public $widgetConfig;
/*
[
'widgetConfig' =>[
'menu' =>[
#
'class' => 'fec\block\TestMenu',
'view' => '@fec/views/testmenu/index.php',
OR
'view' => 'cms/home/index.php',
# Ϊѡ
'method'=> 'getLastData',
'terry1'=> 'My1',
'terry2'=> 'My2',
],
]
]
*/
public function render($configKey){
$config = '';
if(is_array($configKey)){
$config = $configKey;
}else{
if(isset($this->widgetConfig[$configKey])){
$config = $this->widgetConfig[$configKey];
}else{
throw new InvalidValueException(" config key: '$configKey', can not find in ".'Yii::$app->page->widget->widgetConfig'.", you must config it before use it.");
}
}
return $this->renderContent($config);
}
protected function renderContent($config){
if( !isset($config['view']) || empty($config['view'])
){
throw new InvalidConfigException('view and class must exist in array config!');
}
$view = $config['view'];
unset($config['view']);
$viewFile = $this->getViewFile($view);
if( !isset($config['class']) || empty($config['class']))
return Yii::$app->view->render($viewFile, []);
if(isset($config['method']) && !empty($config['method'])){
$method = $config['method'];
unset($config['method']);
}else{
$method = $this->defaultObMethod;
}
$ob = Yii::createObject($config);
$params = $ob->$method();
return Yii::$app->view->renderFile($viewFile, $params);
}
/**
* find theme file by mutil theme ,if not find view file and $throwError=true, it will throw InvalidValueException.
*/
protected function getViewFile($view,$throwError=true){
$view = trim($view);
if(substr($view,0,1) == '@'){
return Yii::getAlias($view);
}
$absoluteDir = Yii::$app->page->theme->getThemeDirArr();
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.'/'.$view;
if(file_exists($file)){
return $file;
}
}
}
/* not find view file */
if($throwError){
$notExistFile = [];
foreach($absoluteDir as $dir){
if($dir){
$file = $dir.'/'.$view;
$notExistFile[] = $file;
}
}
throw new InvalidValueException('view file is not exist in'.implode(',',$notExistFile));
}else{
return false;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册