Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Issue 16 Refactor Routing #336

Merged
merged 40 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
35b0ea1
fixes 16
prisis Aug 6, 2016
e74caa5
micro-optimization
prisis Aug 6, 2016
7a32f98
working on route
prisis Aug 6, 2016
670f058
remove old tests
prisis Aug 7, 2016
182978d
added a ByteCountingStream
prisis Aug 7, 2016
52a6f5f
update interfaces
prisis Aug 7, 2016
362398a
added more functions to route
prisis Aug 8, 2016
f85a692
remove interface
prisis Aug 8, 2016
dc16244
added Container\Traits\ContainerAwareTrait,
prisis Aug 8, 2016
ac762be
fix
prisis Aug 9, 2016
91d2421
Added some tests
prisis Aug 9, 2016
cd173e3
remove rapid route
prisis Aug 9, 2016
df45b96
More work on routing
prisis Aug 10, 2016
e82fcf4
added more tests | start adding benchmarks | adding matcher for uri s…
prisis Aug 12, 2016
7d1cda0
cs fix
prisis Aug 12, 2016
a64168e
fix call for a stdclass VarExporter
prisis Aug 12, 2016
8356efb
cs fixes | remove some magic
prisis Aug 13, 2016
afd1e05
remove some magic
prisis Aug 13, 2016
b247d7a
cs fixes
prisis Aug 13, 2016
b05559e
added new matcher
prisis Aug 13, 2016
5acb592
added all matcher
prisis Aug 13, 2016
59a4058
remove ParameterMatcher | start adding tests
prisis Aug 14, 2016
32aebd7
added more tests
prisis Aug 14, 2016
299740d
more tests
prisis Aug 14, 2016
d4f8145
more tests
prisis Aug 14, 2016
4c364b2
added more tests
prisis Aug 14, 2016
63bab5e
added more tests
prisis Aug 15, 2016
7c5cab8
Scrutinizer Auto-Fixes (#339)
scrutinizer-auto-fixer Aug 15, 2016
a677e60
cs fixes
prisis Aug 15, 2016
dee7c14
Merge branch 'develop' into issue-16-switch_to_RapidRoute
prisis Aug 15, 2016
1d8abad
added all tests for parser
prisis Aug 15, 2016
10c51d6
Cs fixes
prisis Aug 15, 2016
16bfc95
remove need for str class
prisis Aug 15, 2016
db9c7b5
Support command aliases
prisis Aug 15, 2016
781aca4
update dispatcher
prisis Aug 15, 2016
8695267
improve Console
prisis Aug 16, 2016
12015b1
Scrutinizer Auto-Fixes (#342)
scrutinizer-auto-fixer Aug 16, 2016
e246d32
Scrutinizer Auto-Fixes (#343)
scrutinizer-auto-fixer Aug 16, 2016
e6d6baa
cs fixes
prisis Aug 16, 2016
3371e87
Scrutinizer Auto-Fixes (#344)
scrutinizer-auto-fixer Aug 16, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions benchmarks/Routing/RouteParserBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Narrowspark\Routing;

use Benchmark;

class RouteParserBench implements Benchmark
{

}
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@
"narrowspark/http-status" : "^2.0",
"php-di/invoker" : "^1.3",
"phpseclib/phpseclib" : "^2.0",
"nikic/fast-route" : "^0.6",
"ramsey/uuid" : "^3.4",
"paragonie/constant_time_encoding" : "^2.0",
"paragonie/password_lock" : "^3.0",
"psr/cache" : "^1.0",
"psr/log" : "^1.0",
"psr/http-message" : "^1.0",
"psy/psysh" : "^0.6",
"ramsey/uuid" : "^3.4",
"roave/security-advisories" : "dev-master",
"schnittstabil/csrf-tokenservice" : "^2.0",
"stecman/symfony-console-completion" : "^0.6",
Expand Down Expand Up @@ -133,9 +132,10 @@
},
"autoload": {
"psr-4": {
"Viserio\\" : "src/Viserio/"
"Viserio\\" : "src/Viserio/",
"Narrowspark\\" : "benchmarks/"
},
"exclude-from-classmap" : ["src/Viserio/**/Tests/"]
"exclude-from-classmap" : ["src/Viserio/**/Tests/", "benchmarks/"]
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 5 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bootstrap": "vendor/autoload.php",
"path": "benchmarks",
"progress": "dots"
}
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@
<testsuite name="Narrowspark Queue Component Test Suite">
<directory>./src/Viserio/Queue/Tests</directory>
</testsuite>
<!-- <testsuite name="Narrowspark Routing Component Test Suite">
<testsuite name="Narrowspark Routing Component Test Suite">
<directory>./src/Viserio/Routing/Tests</directory>
</testsuite> -->
</testsuite>
<testsuite name="Narrowspark Session Component Test Suite">
<directory>./src/Viserio/Session/Tests</directory>
</testsuite>
Expand Down
2 changes: 1 addition & 1 deletion src/Viserio/Bus/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Viserio\Contracts\Bus\Dispatcher as DispatcherContract;
use Viserio\Pipeline\Pipeline;
use Viserio\Support\Invoker;
use Viserio\Support\Traits\ContainerAwareTrait;
use Viserio\Contracts\Container\Traits\ContainerAwareTrait;

class Dispatcher implements DispatcherContract
{
Expand Down
1 change: 0 additions & 1 deletion src/Viserio/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Memcached;
use MongoDB\Driver\Manager as MongoDBManager;
use Predis\Client as PredisClient;
use Psr\Cache\CacheItemPoolInterface;
use Redis;
use Viserio\{
Filesystem\FilesystemManager,
Expand Down
10 changes: 5 additions & 5 deletions src/Viserio/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
],
"require": {
"php" : "7.0.0 - 7.0.5 || ^7.0.7",
"cache/chain-adapter" : "^0.3",
"cache/chain-adapter" : "^0.4",
"cache/namespaced-cache" : "^0.1",
"psr/cache" : "^1.0",
"viserio/config" : "self.version",
"viserio/cotracts" : "self.version"
},
"require-dev": {
"cache/array-adapter" : "^0.2",
"cache/array-adapter" : "^0.4",
"cache/filesystem-adapter" : "^0.3",
"cache/session-handler" : "^0.1",
"cache/session-handler" : "^0.2",
"cache/void-adapter" : "^0.3",
"narrowspark/php-cs-fixer-config" : "^1.1",
"narrowspark/testing-helper" : "^1.5",
Expand Down Expand Up @@ -76,13 +76,13 @@
"suggest": {
"cache/apc-adapter" : "Required to use the Apc cache (^0.3).",
"cache/apcu-adapter" : "Required to use the Apcu cache (^0.3).",
"cache/array-adapter" : "Required to use the Array cache (^0.2)",
"cache/array-adapter" : "Required to use the Array cache (^0.4)",
"cache/filesystem-adapter" : "Required to use the Filesystem cache (^0.3).",
"cache/memcache-adapter" : "Required to use the Memcache cache (^0.3).",
"cache/memcached-adapter" : "Required to use the Memcached cache (^0.3).",
"cache/mongodb-adapter" : "Required to use the Mongodb cache (^0.2).",
"cache/predis-adapter" : "Required to use the Predis cache (^0.4).",
"cache/session-handler" : "Required to use the Session cache (^0.1).",
"cache/session-handler" : "Required to use the Session cache (^0.2).",
"cache/void-adapter" : "Required to use the Void cache (^0.3)."
},
"minimum-stability" : "dev",
Expand Down
4 changes: 2 additions & 2 deletions src/Viserio/Connect/ConnectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ protected function createMariadbConnection(array $config): PDO
*
* @param array $config
*
* @return \Mongo|\MongoClient|\MongoDB\Client
* @return \Mongo
*/
protected function createMongoConnection(array $config): PDO
protected function createMongoConnection(array $config)
{
return (new MongoConnector())->connect($config);
}
Expand Down
60 changes: 36 additions & 24 deletions src/Viserio/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@
declare(strict_types=1);
namespace Viserio\Console;

use Closure;
use Interop\Container\ContainerInterface as ContainerContract;
use Invoker\Exception\InvocationException;
use RuntimeException;
use Symfony\Component\Console\Application as SymfonyConsole;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Viserio\Console\Command\Command as ViserioCommand;
use Viserio\Console\Command\ExpressionParser as Parser;
use Viserio\Console\Input\InputOption;
use Viserio\Contracts\Console\Application as ApplicationContract;
use Symfony\Component\Console\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

Application as SymfonyConsole,
Command\Command as SymfonyCommand,
Input\InputDefinition,
Input\InputInterface,
Output\OutputInterface
};
use Viserio\Console\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

Command\Command as ViserioCommand,
Command\ExpressionParser as Parser,
Input\InputOption
};
use Viserio\Contracts\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

Console\Application as ApplicationContract,
Container\Traits\ContainerAwareTrait
};
use Viserio\Support\Invoker;
use Viserio\Support\Traits\ContainerAwareTrait;

class Application extends SymfonyConsole implements ApplicationContract
{
Expand Down Expand Up @@ -59,9 +66,9 @@ class Application extends SymfonyConsole implements ApplicationContract
/**
* Create a new Cerebro console application.
*
* @param ContainerContract $container
* @param string $version
* @param string $name
* @param \Interop\Container\ContainerInterface $container
* @param string $version
* @param string $name
*/
public function __construct(
ContainerContract $container,
Expand All @@ -70,23 +77,22 @@ public function __construct(
) {
$this->name = $name;
$this->version = $version;

$this->setContainer($container);
$this->container = $container;
$this->expressionParser = new Parser();
$this->initInvoker();

$this->initInvoker();
$this->setAutoExit(false);
$this->setCatchExceptions(false);

parent::__construct($this->getName(), $this->getVersion());
parent::__construct($name, $version);
}

/**
* Add a command to the console.
*
* @param \Symfony\Component\Console\Command\Command $command
*
* @return SymfonyCommand|null
* @return null|\Symfony\Component\Console\Command\Command
*/
public function add(SymfonyCommand $command)
{
Expand All @@ -105,10 +111,11 @@ public function add(SymfonyCommand $command)
* @param callable|string|array $callable Called when the command is called.
* When using a container, this can be a "pseudo-callable"
* i.e. the name of the container entry to invoke.
* @param array $aliases An array of aliases for the command.
*
* @return SymfonyCommand
* @return \Symfony\Component\Console\Command\Command
*/
public function command(string $expression, $callable): SymfonyCommand
public function command(string $expression, $callable, array $aliases = []): SymfonyCommand
{
$commandFunction = function (InputInterface $input, OutputInterface $output) use ($callable) {
$parameters = array_merge(
Expand All @@ -120,18 +127,23 @@ public function command(string $expression, $callable): SymfonyCommand
$input->getOptions()
);

if ($callable instanceof Closure) {
$callable = $callable->bindTo($this, $this);
}

try {
$this->getInvoker()->call($callable, $parameters);
} catch (InvocationException $e) {
} catch (InvocationException $exception) {
throw new RuntimeException(sprintf(
"Impossible to call the '%s' command: %s",
$input->getFirstArgument(),
$e->getMessage()
), 0, $e);
$exception->getMessage()
), 0, $exception);
}
};

$command = $this->createCommand($expression, $commandFunction);
$command->setAliases($aliases);

$this->add($command);

Expand Down Expand Up @@ -208,7 +220,7 @@ protected function getEnvironmentOption(): InputOption
* @param string $expression
* @param callable $callable
*
* @return SymfonyCommand
* @return \Symfony\Component\Console\Command\Command
*/
protected function createCommand(string $expression, callable $callable): SymfonyCommand
{
Expand Down
8 changes: 4 additions & 4 deletions src/Viserio/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
Question\Question
};
use Viserio\Console\Style\NarrowsparkStyle;
use Viserio\Contracts\Support\Arrayable;
use Viserio\Support\{
Invoker,
Traits\ContainerAwareTrait
use Viserio\Contracts\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

Support\Arrayable,
Container\Traits\ContainerAwareTrait
};
use Viserio\Support\Invoker;

abstract class Command extends BaseCommand implements CompletionAwareInterface
{
Expand Down
8 changes: 5 additions & 3 deletions src/Viserio/Console/Command/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
declare(strict_types=1);
namespace Viserio\Console\Command;

use Viserio\Console\Input\InputArgument;
use Viserio\Console\Input\InputOption;
use Viserio\Contracts\Console\InvalidCommandExpression;
use Viserio\Console\Input\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

InputArgument,
InputOption
};
use Viserio\Contracts\Console\Exceptions\InvalidCommandExpression;
use Viserio\Support\Str;

class ExpressionParser
Expand Down
51 changes: 41 additions & 10 deletions src/Viserio/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

use Mockery as Mock;
use Narrowspark\TestingHelper\ArrayContainer;
use stdClass;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Viserio\Console\Application;
use Viserio\Console\Tests\Fixture\SpyOutput;
use Viserio\Console\Tests\Fixture\ViserioCommand;
use StdClass;
use Symfony\Component\Console\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

Input\StringInput,
Output\OutputInterface
};
use Viserio\Console\{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one USE keyword per declaration

Application,
Tests\Fixture\SpyOutput,
Tests\Fixture\ViserioCommand
};

class ApplicationTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -20,9 +24,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$stdClass = new stdClass();
$stdClass = new StdClass();
$stdClass->foo = 'hello';
$stdClass2 = new stdClass();
$stdClass2 = new StdClass();
$stdClass2->foo = 'nope!';

$container = new ArrayContainer([
Expand Down Expand Up @@ -139,7 +143,7 @@ public function testItShouldRunACommandWitMultipleOptions()

public function testItShouldInjectTypeHintInPriority()
{
$this->application->command('greet', function (OutputInterface $output, stdClass $param) {
$this->application->command('greet', function (OutputInterface $output, StdClass $param) {
$output->write($param->foo);
});

Expand All @@ -149,20 +153,23 @@ public function testItShouldInjectTypeHintInPriority()
public function testItCanResolveCallableStringFromContainer()
{
$this->application->command('greet', 'command.greet');

$this->assertOutputIs('greet', 'hello');
}

public function testItCanResolveCallableArrayFromContainer()
{
$this->application->command('greet', 'command.arr.greet');

$this->assertOutputIs('greet', 'hello');
}

public function testItcanInjectUsingTypeHints()
{
$this->application->command('greet', function (OutputInterface $output, stdClass $stdClass) {
$this->application->command('greet', function (OutputInterface $output, StdClass $stdClass) {
$output->write($stdClass->foo);
});

$this->assertOutputIs('greet', 'hello');
}

Expand All @@ -171,6 +178,7 @@ public function testItCanInjectUsingParameterNames()
$this->application->command('greet', function (OutputInterface $output, $stdClass) {
$output->write($stdClass->foo);
});

$this->assertOutputIs('greet', 'hello');
}

Expand All @@ -182,9 +190,31 @@ public function testItShouldThrowIfAParameterCannotBeResolved()
{
$this->application->command('greet', function ($fbo) {
});

$this->assertOutputIs('greet', '');
}

public function testRunsACommandViaItsAliasAndReturnsExitCode()
{
$this->application->command('foo', function ($output) {
$output->write(1);
}, ['bar']);

$this->assertOutputIs('bar', 1);
}

public function testitShouldRunACommandInTheScopeOfTheApplication()
{
$whatIsThis = null;

$this->application->command('foo', function () use (&$whatIsThis) {
$whatIsThis = $this;
});

$this->assertOutputIs('foo', '');
$this->assertSame($this->application, $whatIsThis);
}

/**
* Fixture method.
*
Expand All @@ -204,6 +234,7 @@ private function assertOutputIs($command, $expected)
$output = new SpyOutput();

$this->application->run(new StringInput($command), $output);

$this->assertEquals($expected, $output->output);
}
}
Loading