From ff5c16da713006f02ff8790f862481ba57c4d315 Mon Sep 17 00:00:00 2001 From: inhere Date: Fri, 17 Jan 2020 19:42:47 +0800 Subject: [PATCH] update some logic for application start --- .../src/Resource/AnnotationResource.php | 2 +- src/console/src/Input/Input.php | 2 +- src/console/test/unit/Input/InputTest.php | 3 + src/framework/run.php | 25 ++--- src/framework/src/Concern/SwoftTrait.php | 102 ++++++++++++++++++ src/framework/src/Contract/SwoftInterface.php | 2 - .../src/Processor/ConsoleProcessor.php | 4 +- src/framework/src/Swoft.php | 2 +- src/framework/src/SwoftApplication.php | 61 +---------- .../test/testing/TestApplication.php | 30 ++---- src/i18n/test/testing/TestApplication.php | 1 - src/tcp-server/test/bootstrap.php | 29 ----- 12 files changed, 130 insertions(+), 133 deletions(-) diff --git a/src/annotation/src/Resource/AnnotationResource.php b/src/annotation/src/Resource/AnnotationResource.php index 83769476e..75115712a 100644 --- a/src/annotation/src/Resource/AnnotationResource.php +++ b/src/annotation/src/Resource/AnnotationResource.php @@ -122,7 +122,7 @@ public function __construct(array $config = []) { // Init $excludedPsr4Prefixes $this->excludedPsr4Prefixes = self::DEFAULT_EXCLUDED_PSR4_PREFIXES; - + // Can set property by array ObjectHelper::init($this, $config); diff --git a/src/console/src/Input/Input.php b/src/console/src/Input/Input.php index 5db18fa0f..7a396d080 100644 --- a/src/console/src/Input/Input.php +++ b/src/console/src/Input/Input.php @@ -63,7 +63,7 @@ public function __construct(array $args = null, bool $parsing = true) if ($parsing) { // list($this->args, $this->sOpts, $this->lOpts) = InputParser::fromArgv($args); - [$this->args, $this->sOpts, $this->lOpts] = Flags::parseArgv($args); + [$this->args, $this->sOpts, $this->lOpts] = Flags::parseArgv($this->flags); } } diff --git a/src/console/test/unit/Input/InputTest.php b/src/console/test/unit/Input/InputTest.php index 9dec7ba04..dcd096802 100644 --- a/src/console/test/unit/Input/InputTest.php +++ b/src/console/test/unit/Input/InputTest.php @@ -14,6 +14,9 @@ public function testInput(): void $in = new Input($args); $this->assertSame('input:test', $in->getCommand()); + $this->assertSame([], $in->getArgs()); + $this->assertSame(['-d', '12'], $in->getFlags()); + $this->assertSame(['d' => '12'], $in->getOpts()); } /** diff --git a/src/framework/run.php b/src/framework/run.php index c56feecb0..f4841bb88 100644 --- a/src/framework/run.php +++ b/src/framework/run.php @@ -36,28 +36,21 @@ } unset($file); if (!defined('PHPUNIT_COMPOSER_INSTALL')) { - fwrite(STDERR, - 'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL . ' composer install' . PHP_EOL . PHP_EOL . 'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL); + $tips = << 1, + * ] + * + * @var array + */ + private $disabledProcessors = []; + + /** + * Can disable AutoLoader class before handle. + * eg. + * [ + * Swoft\Console\AutoLoader::class => 1, + * ] + * + * @var array + */ + private $disabledAutoLoaders = []; + + /** + * Scans containing these namespace prefixes will be excluded. + * + * @var array + * eg. + * [ + * 'PHPUnit\\', + * ] + */ + private $disabledPsr4Prefixes = []; + /** * Get env name * @@ -186,4 +224,68 @@ public function afterConsole(): bool { return true; } + + /** + * @return array + */ + public function getDisabledProcessors(): array + { + return $this->disabledProcessors; + } + + /** + * @return array + */ + public function getDisabledAutoLoaders(): array + { + return $this->disabledAutoLoaders; + } + + /** + * @return array + */ + public function getDisabledPsr4Prefixes(): array + { + return $this->disabledPsr4Prefixes; + } + + /** + * @param array $disabledAutoLoaders + */ + public function setDisabledAutoLoaders(array $disabledAutoLoaders): void + { + $this->disabledAutoLoaders = $disabledAutoLoaders; + } + + /** + * @param array $disabledPsr4Prefixes + */ + public function setDisabledPsr4Prefixes(array $disabledPsr4Prefixes): void + { + $this->disabledPsr4Prefixes = $disabledPsr4Prefixes; + } + + /** + * @param array $disabledProcessors + */ + public function setDisabledProcessors(array $disabledProcessors): void + { + $this->disabledProcessors = $disabledProcessors; + } + + /** + * @return bool + */ + public function isStartConsole(): bool + { + return $this->startConsole; + } + + /** + * @param bool $startConsole + */ + public function setStartConsole($startConsole): void + { + $this->startConsole = (bool)$startConsole; + } } diff --git a/src/framework/src/Contract/SwoftInterface.php b/src/framework/src/Contract/SwoftInterface.php index 4646e830f..594d317cf 100644 --- a/src/framework/src/Contract/SwoftInterface.php +++ b/src/framework/src/Contract/SwoftInterface.php @@ -11,8 +11,6 @@ */ interface SwoftInterface { - public const VERSION = '1.0.0'; - /** * Get env name * diff --git a/src/framework/src/Processor/ConsoleProcessor.php b/src/framework/src/Processor/ConsoleProcessor.php index 87bbde2ef..a61adf418 100644 --- a/src/framework/src/Processor/ConsoleProcessor.php +++ b/src/framework/src/Processor/ConsoleProcessor.php @@ -36,7 +36,9 @@ public function handle(): bool CLog::info('Console command route registered (group %d, command %d)', $router->groupCount(), $router->count()); // Run console application - bean('cliApp')->run(); + if ($this->application->isStartConsole()) { + bean('cliApp')->run(); + } return $this->application->afterConsole(); } diff --git a/src/framework/src/Swoft.php b/src/framework/src/Swoft.php index f250c53a0..f634f597f 100644 --- a/src/framework/src/Swoft.php +++ b/src/framework/src/Swoft.php @@ -25,7 +25,7 @@ final class Swoft /** * Swoft version */ - public const VERSION = '2.0.7'; + public const VERSION = '2.0.8'; /** * Swoft terminal logo diff --git a/src/framework/src/SwoftApplication.php b/src/framework/src/SwoftApplication.php index 3a0f4c3b2..addadcbac 100644 --- a/src/framework/src/SwoftApplication.php +++ b/src/framework/src/SwoftApplication.php @@ -91,47 +91,14 @@ class SwoftApplication implements SwoftInterface, ApplicationInterface */ private $processor; - /** - * Can disable processor class before handle. - * eg. - * [ - * Swoft\Processor\ConsoleProcessor::class => 1, - * ] - * - * @var array - */ - private $disabledProcessors = []; - - /** - * Can disable AutoLoader class before handle. - * eg. - * [ - * Swoft\Console\AutoLoader::class => 1, - * ] - * - * @var array - */ - private $disabledAutoLoaders = []; - - /** - * Scans containing these namespace prefixes will be excluded. - * - * @var array - * eg. - * [ - * 'PHPUnit\\', - * ] - */ - private $disabledPsr4Prefixes = []; - /** * Get the application version * * @return string */ - public static function getVersion(): string + public function getVersion(): string { - return self::VERSION; + return Swoft::VERSION; } /** @@ -304,30 +271,6 @@ protected function processors(): array ]; } - /** - * @return array - */ - public function getDisabledProcessors(): array - { - return $this->disabledProcessors; - } - - /** - * @return array - */ - public function getDisabledAutoLoaders(): array - { - return $this->disabledAutoLoaders; - } - - /** - * @return array - */ - public function getDisabledPsr4Prefixes(): array - { - return $this->disabledPsr4Prefixes; - } - /** * @param string $beanFile */ diff --git a/src/framework/test/testing/TestApplication.php b/src/framework/test/testing/TestApplication.php index e7f0ee87b..e43a5a63b 100644 --- a/src/framework/test/testing/TestApplication.php +++ b/src/framework/test/testing/TestApplication.php @@ -2,11 +2,6 @@ namespace SwoftTest\Testing; -use Swoft\Processor\AnnotationProcessor; -use Swoft\Processor\BeanProcessor; -use Swoft\Processor\ConfigProcessor; -use Swoft\Processor\EnvProcessor; -use Swoft\Processor\EventProcessor; use Swoft\SwoftApplication; /** @@ -16,6 +11,14 @@ */ class TestApplication extends SwoftApplication { + public function __construct(array $config = []) + { + // tests: disable run console application + $this->setStartConsole(false); + + parent::__construct($config); + } + public function getCLoggerConfig(): array { $config = parent::getCLoggerConfig(); @@ -25,21 +28,4 @@ public function getCLoggerConfig(): array return $config; } - - /** - * Rewrite processors - * - * @return array - */ - protected function processors(): array - { - return [ - new EnvProcessor($this), - new ConfigProcessor($this), - new AnnotationProcessor($this), - new BeanProcessor($this), - new EventProcessor($this), - new ConsoleProcessor($this), - ]; - } } diff --git a/src/i18n/test/testing/TestApplication.php b/src/i18n/test/testing/TestApplication.php index 08f39c286..c81e10f7f 100644 --- a/src/i18n/test/testing/TestApplication.php +++ b/src/i18n/test/testing/TestApplication.php @@ -1,6 +1,5 @@ exec($php, [ $dir . '/test/bin/swoft', 'ws:start'); - $proc->exec($php, ['test/bin/swoft', $type . ':start']); - }); - $pid = $proc->start(); - echo "Swoft test server started, PID $pid\n"; - - // wait server starting... - sleep(2); - echo file_get_contents('http://127.0.0.1:28308/hi'); -} - $application = new TestApplication([ 'basePath' => __DIR__ ]); $application->setBeanFile(__DIR__ . '/testing/bean.php'); $application->run(); - -if (isset($pid) && $pid > 0) { - echo "Stop server on tests end. PID $pid"; - $ok = Process::kill($pid, 15); - echo $ok ? " OK\n" : " FAIL\n"; -}