Skip to content

Commit

Permalink
NOT FINISHED implementing Yii 2.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Mar 5, 2018
1 parent f6cc4f8 commit 20183c1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/base/Starter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use hidev\components\Request;
use hidev\helpers\ConfigPlugin;
use hidev\helpers\FileHelper;
use hidev\helpers\Helper;
use Symfony\Component\Yaml\Yaml;
use Yii;
use yii\base\InvalidParamException;
Expand Down Expand Up @@ -135,9 +136,13 @@ public function addAutoloader()
{
$autoloader = './vendor/autoload.php';
if (file_exists($autoloader)) {
spl_autoload_unregister(['Yii', 'autoload']);
if (Helper::isYii20()) {
spl_autoload_unregister(['Yii', 'autoload']);
}
require $autoloader;
spl_autoload_register(['Yii', 'autoload'], true, true);
if (Helper::isYii20()) {
spl_autoload_register(['Yii', 'autoload'], true, true);
}
}
}

Expand Down
41 changes: 30 additions & 11 deletions src/config/basis.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,59 @@
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
*/

use hidev\helpers\Helper;

if (!defined('HIDEV_VENDOR_DIR')) {
define('HIDEV_VENDOR_DIR', dirname(dirname(dirname(dirname(__DIR__)))));
}

$__class = Helper::isYii20() ? 'class' : '__class';

$runtimePath = (substr(__DIR__, 0, 7) === 'phar://' ? dirname($_SERVER['SCRIPT_NAME']) : dirname(HIDEV_VENDOR_DIR)) . '/.hidev/runtime';

return [
return array_filter([
'id' => 'hidev',
'name' => 'HiDev',
'basePath' => dirname(__DIR__),
'vendorPath' => HIDEV_VENDOR_DIR,
'runtimePath' => $runtimePath,
'controllerNamespace' => 'hidev\\console',
'defaultRoute' => 'default',
'bootstrap' => ['log'],
'components' => [
'log' => [
'bootstrap' => Helper::isYii20() ? ['log'] : null,
'logger' => Helper::isYii20() ? null : [
'flushInterval' => 1,
'targets' => [
'console' => [
$__class => \hidev\log\ConsoleTarget::class,
'levels' => ['error', 'warning', 'info'],
],
],
],
'components' => array_filter([
'log' => Helper::isYii20() ? [
'class' => \hidev\components\Log::class,
'flushInterval' => 1,
'targets' => [
'console' => [
'class' => \hidev\log\ConsoleTarget::class,
'class' => \hidev\log\OldConsoleTarget::class,
'levels' => ['error', 'warning', 'info'],
],
],
],
] : null,
'request' => [
'class' => \hidev\components\Request::class,
$__class => \hidev\components\Request::class,
],
'cache' => [
'cache' => Helper::isYii20() ? [
'class' => \yii\caching\FileCache::class,
] : [
$__class => \yii\caching\Cache::class,
'handler' => [
$__class => \yii\caching\FileCache::class,
]
],
'view' => [
'class' => \hidev\components\View::class,
$__class => \hidev\components\View::class,
'renderers' => [
'twig' => [
'class' => \yii\twig\ViewRenderer::class,
Expand Down Expand Up @@ -88,7 +107,7 @@
'class' => \hidev\components\Version::class,
'file' => dirname(dirname(__DIR__)) . '/version',
],
],
]),
'controllerMap' => [
'--version' => [
'class' => \hidev\console\VersionController::class,
Expand Down Expand Up @@ -117,4 +136,4 @@
'directory' => \hidev\components\Directory::class,
],
],
];
]);
2 changes: 1 addition & 1 deletion src/console/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CommonController extends \hidev\base\Controller

public function actionIndex()
{
Yii::trace("Started: '$this->id'");
Yii::debug("Started: '$this->id'");
}

public function actions()
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
namespace hidev\helpers;

use hiqdev\php\collection\ArrayHelper;
use Yii;
use yii\helpers\Inflector;

/**
* Hidev Helper.
*/
class Helper
{
public static function isYii20()
{
return method_exists(Yii::class, 'autoload');
}

public static function bad2sep($str, $separator = '-')
{
return preg_replace('/[^a-zA-Z0-9-]/', $separator, $str);
Expand Down
20 changes: 10 additions & 10 deletions src/log/ConsoleTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace hidev\log;

use Psr\Log\LogLevel;
use Yii;
use yii\helpers\Console;
use yii\log\Logger;
Expand All @@ -19,8 +20,12 @@ class ConsoleTarget extends \yii\log\Target
public $exportInterval = 1;

public static $styles = [
Logger::LEVEL_WARNING => [Console::FG_YELLOW],
Logger::LEVEL_ERROR => [Console::FG_RED],
LogLevel::EMERGENCY => [Console::FG_RED],
LogLevel::ALERT => [Console::FG_RED],
LogLevel::CRITICAL => [Console::FG_RED],
LogLevel::ERROR => [Console::FG_RED],
LogLevel::WARNING => [Console::FG_YELLOW],
LogLevel::NOTICE => [Console::FG_YELLOW],
];

public function export()
Expand All @@ -30,23 +35,18 @@ public function export()
}
}

public function out($message, $level)
public function out($level, $message)
{
if ($level > $this->getLevel()) {
/*if ($level > $this->getLevel()) {
return;
}
}*/
$style = self::$styles[$level];
if ($style) {
$message = Console::ansiFormat($message, $style);
}
Console::stdout($message . "\n");
}

public function getLevel()
{
return Yii::$app->get('log')->getLevel();
}

protected function getContextMessage()
{
return '';
Expand Down
54 changes: 54 additions & 0 deletions src/log/OldConsoleTarget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Automation tool mixed with code generator for easier continuous development
*
* @link https://github.com/hiqdev/hidev
* @package hidev
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
*/

namespace hidev\log;

use Yii;
use yii\helpers\Console;
use yii\log\Logger;

class OldConsoleTarget extends \yii\log\Target
{
public $exportInterval = 1;

public static $styles = [
Logger::LEVEL_WARNING => [Console::FG_YELLOW],
Logger::LEVEL_ERROR => [Console::FG_RED],
];

public function export()
{
foreach ($this->messages as $message) {
$this->out($message[0], $message[1]);
}
}

public function out($message, $level)
{
if ($level > $this->getLevel()) {
return;
}
$style = self::$styles[$level];
if ($style) {
$message = Console::ansiFormat($message, $style);
}
Console::stdout($message . "\n");
}

public function getLevel()
{
return Yii::$app->get('log')->getLevel();
}

protected function getContextMessage()
{
return '';
}
}

0 comments on commit 20183c1

Please sign in to comment.