Skip to content

Commit

Permalink
performance improvements on Router, removed ancestor resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
eguvenc committed Aug 16, 2017
1 parent 3c63e96 commit 20733cc
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 268 deletions.
2 changes: 0 additions & 2 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ $container->share('request', Obullo\ServerRequestFactory::fromGlobals());
* Create your console commands
*/
use Symfony\Component\Console\Application;
use AppBundle\Command\InstallCommand;
use AppBundle\Command\LogHttpCommand;

$application = new Application();
$application->add(new InstallCommand($container));
$application->add(new LogHttpCommand($container));
$application->run();
6 changes: 3 additions & 3 deletions public/dev_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@

$container->share('request', Obullo\ServerRequestFactory::fromGlobals());
$container->share('response', new Zend\Diactoros\Response);
$container->share('router', new Obullo\Router\Router($container, ['autoResolver' => true]));
$container->share('router', new Obullo\Router\Router($container));

/**
* Step 3: Create your mvc application
*/
$application = new Obullo\Mvc\BenchmarkableApp($container);
$application = new Obullo\Mvc\BenchmarkAwareApp($container);

$application->addRouteableBundle(new AppBundle\IndexBundle('/'));
// $application->addRouteableBundle(new BackendBundle\IndexBundle('/backend'));
$application->create();
$application->start();

/**
* Step 4: Define your server using Zend Diactoros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Interop\Container\ContainerInterface as Container;

class InstallCommand extends Command
class GenerateBundleCommand extends Command
{
public function __construct(Container $container)
{
Expand All @@ -18,8 +18,8 @@ public function __construct(Container $container)
protected function configure()
{
$this
->setName('install:app')
->setDescription('Install Service Providers.');
->setName('bundle:generate')
->setDescription('Generate your bundle');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Command/LogHttpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (is_file($file)) {
unlink($file);
}
$output->writeln('<info>Log file delete successfully.</info>');
$output->writeln('<info>Log file deleted successfully.</info>');
return;
}
/**
Expand Down
3 changes: 3 additions & 0 deletions src/AppBundle/Controller/WelcomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class WelcomeController extends Controller
*/
public function indexAction($request)
{

// print_r($request->getArgs());

// $this->console->log("test");

return new HtmlResponse($this->render('welcome.phtml'));
Expand Down
5 changes: 0 additions & 5 deletions src/AppBundle/Middleware/FinalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class FinalHandler implements ContainerAwareInterface
*/
public function __invoke(Request $request, Response $response, $err = null, $handler = null)
{
/*
if ($err) {
return $this->handleError($err);
}
*/
if ($handler instanceof Response) {
return $handler;
}
Expand Down
13 changes: 7 additions & 6 deletions src/AppBundle/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
$router->rewrite('GET', '(?:en|de|es|tr)|/(.*)', '$1'); // example.com/en/ (or) // example.com/en

$router->map('GET', '/', 'welcome/index');
$router->map('GET', '/welcome', 'welcome/index');
$router->map('GET', '/welcome/index/(\d+)', '/welcome/index/$1');
// $router->map('GET', '/|welcome', 'welcome/index');

// $router->map('GET', '/users/(\w+)/(\d+)', '/users/$1/$2');
Expand All @@ -37,13 +39,13 @@ function () use ($router) {
'test/',
function () use ($router) {

// $router->map('GET', '/users/(\w+)/(\d+)', '/users/$1/$2');
// $router->map('GET', '/users/test/(\w+)/(\d+)', '/welcome/index/$1/$2');

// throw new \Exception("cimcime");

$router->map(
'GET',
'/users/(\w+)/(\d+).*',
'/users/test/(\w+)/(\d+).*',
function ($request, $response, $args) {

// var_dump($args);
Expand All @@ -54,6 +56,7 @@ function ($request, $response, $args) {
}
)->add('Guest');


//->filter('contains', ['/users/test/45'])->add('Guest');

//->filter('notContains', ['/users/teZ'])->add('Guest');;
Expand All @@ -62,9 +65,7 @@ function ($request, $response, $args) {
// ->ifNotContains(['login', 'payment'])
// ->ifRegExp(['welcome/path/index'])
// ->ifNotRegExp(['welcome/path/index'])

}
);

}
);
);
6 changes: 3 additions & 3 deletions src/Obullo/Mvc/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public function addRouteableBundle(Bundle $bundle)
}

/**
* Create routeable application bundles
* Start application & create routeable bundles
*
* @return void
*/
public function create()
public function start()
{
if (empty($this->bundles[0])) {
die("Bundle could not be initialized. Check your '".getenv("APP_ENV")."_app.php' file.");
Expand Down Expand Up @@ -157,7 +157,7 @@ public function __invoke(Request $request, Response $response)
}
$resolver = new ControllerResolver($this->container, $request, $response);
$result = $resolver->dispatch($handler);

if (! $result) {
$notFound = new $notFoundMiddleware;
$notFound->setContainer($this->container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @copyright 2009-2017 Obullo
* @license http:https://opensource.org/licenses/MIT MIT license
*/
class BenchmarkableApp extends App
class BenchmarkAwareApp extends App
{
/**
* Benchmark timer
Expand Down
52 changes: 5 additions & 47 deletions src/Obullo/Mvc/ControllerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ControllerResolver
{
protected $router;
protected $folder = 'Controller';
protected $subfolderLevel = 3;

/**
* Constructor
Expand Down Expand Up @@ -65,13 +64,9 @@ public function dispatch($handler)
if (! empty($segments[$method])) {
$this->router->setMethod($segments[$method]); // A standard method request
} else {
$segments[$method] = 'index'; // This lets the "routed" segment array identify
// that the default index method is being used.
$this->router->setMethod('index');
}
$this->arity = (3 + $arity);

return $this->call($segments);
return $this->call();
}

/**
Expand All @@ -86,16 +81,6 @@ public function setFolder($folder)
$this->folder = $folder;
}

/**
* Returns to subfolder level
*
* @return integer
*/
public function getSubfolderLevel()
{
return $this->subfolderLevel;
}

/**
* Resolve segments
*
Expand All @@ -109,13 +94,7 @@ protected function resolve($segments)
return null;
}
$this->router->setFolder($segments[0]); // Set first segment as default folder
$segments = $this->checkAncestor($segments);
$ancestor = $this->router->getAncestor('/');

if (! empty($ancestor)) {
$resolver = new AncestorResolver($this->router, $this->getSubfolderLevel());
return $resolver->resolve($segments);
}
if (is_dir(APP_PATH . $this->folder .'/'. $this->router->getFolder())) {
$resolver = new FolderResolver($this->router);
return $resolver->resolve($segments);
Expand All @@ -125,24 +104,6 @@ protected function resolve($segments)
return $resolver->resolve($segments);
}

/**
* Check first segment if have a ancestor folder & set it.
*
* @param array $segments uri segments
*
* @return array
*/
protected function checkAncestor($segments)
{
if (! empty($segments[1])
&& is_dir(APP_PATH . $this->folder .'/'. $segments[0] .'/'. $segments[1].'/') // Detect ancestor
) {
$this->router->setAncestor($segments[0]);
array_shift($segments);
}
return $segments;
}

/**
* Returns to arity
*
Expand All @@ -160,7 +121,7 @@ public function getArity()
*/
public function getFilename()
{
return APP_PATH . $this->folder .'/'. $this->router->getAncestor('/') . $this->router->getFolder('/') . $this->router->getClass() . 'Controller.php';
return APP_PATH . $this->folder .'/'. $this->router->getFolder('/') . $this->router->getClass() . 'Controller.php';
}

/**
Expand All @@ -176,11 +137,9 @@ public function getNamespace()
/**
* Call the controller
*
* @param array $segments path segments
*
* @return string|Response
*/
public function call($segments)
public function call()
{
$request = $this->container->get('request');
$file = $this->getFilename();
Expand All @@ -200,9 +159,8 @@ public function call($segments)
return false;
}
}
$args = array_slice($segments, $this->getArity());
$request->setArgs($args);
$request->setArgs($this->router->getParams());

return $controller->$method($request);
}
}
}
Loading

0 comments on commit 20733cc

Please sign in to comment.