Skip to content

Commit

Permalink
update some logic for application start
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 17, 2020
1 parent 5372179 commit ff5c16d
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/annotation/src/Resource/AnnotationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/console/src/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/console/test/unit/Input/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
25 changes: 9 additions & 16 deletions src/framework/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<TXT
You need to set up the project dependencies using Composer:
composer install
You can learn all about Composer on https://getcomposer.org/
TXT;

fwrite(STDERR, $tips . PHP_EOL);
die(1);
} else {
if (array_reverse(explode('/', __DIR__))[0] ?? '' === 'tests') {
$vendor_dir = dirname(PHPUNIT_COMPOSER_INSTALL);
$bin_unit = "{$vendor_dir}/bin/phpunit";
$unit_uint = "{$vendor_dir}/phpunit/phpunit/phpunit";
if (file_exists($bin_unit)) {
@unlink($bin_unit);
@symlink(__FILE__, $bin_unit);
}
if (file_exists($unit_uint)) {
@unlink($unit_uint);
@symlink(__FILE__, $unit_uint);
}
}
}

if (!in_array('-c', $_SERVER['argv'])) {
$_SERVER['argv'][] = '-c';
$_SERVER['argv'][] = __DIR__ . '/phpunit.xml';
}

require PHPUNIT_COMPOSER_INSTALL;

$status = 0;
Expand Down
102 changes: 102 additions & 0 deletions src/framework/src/Concern/SwoftTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@
*/
trait SwoftTrait
{
/**
* @var bool
*/
protected $startConsole = true;

/**
* 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 env name
*
Expand Down Expand Up @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/framework/src/Contract/SwoftInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*/
interface SwoftInterface
{
public const VERSION = '1.0.0';

/**
* Get env name
*
Expand Down
4 changes: 3 additions & 1 deletion src/framework/src/Processor/ConsoleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/framework/src/Swoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Swoft
/**
* Swoft version
*/
public const VERSION = '2.0.7';
public const VERSION = '2.0.8';

/**
* Swoft terminal logo
Expand Down
61 changes: 2 additions & 59 deletions src/framework/src/SwoftApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
30 changes: 8 additions & 22 deletions src/framework/test/testing/TestApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand All @@ -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),
];
}
}
1 change: 0 additions & 1 deletion src/i18n/test/testing/TestApplication.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php declare(strict_types=1);


namespace SwoftTest\I18n\Testing;

/**
Expand Down
29 changes: 0 additions & 29 deletions src/tcp-server/test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,8 @@
exit('Please run "composer install" to install the dependencies' . PHP_EOL);
}

// php run.php -c src/tcp-server/phpunit.xml
// SWOFT_TEST_TCP_SERVER=1
if (1 === (int)getenv('SWOFT_TEST_TCP_SERVER')) {
// Output: "php is /usr/local/bin/php"
[$ok, $ret,] = Sys::run('type php');
if (0 !== $ok) {
exit('php not found');
}

$type = 'tcp';
$php = substr(trim($ret), 7);
$proc = new Process(function (Process $proc) use ($php, $type) {
// $proc->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";
}

0 comments on commit ff5c16d

Please sign in to comment.