Skip to content

Commit

Permalink
Added actions caching
Browse files Browse the repository at this point in the history
  • Loading branch information
desh901 committed Sep 13, 2017
1 parent b93d5cf commit 2a4756a
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ test.*
server
lang
cache
bootstrap
bootstrap
routes
4 changes: 2 additions & 2 deletions config/hermes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
\Illuminate\Cache\CacheServiceProvider::class,
\Hermes\Providers\ContextServiceProvider::class,
\Hermes\Providers\CredentialsServiceProvider::class,
\Hermes\Providers\ActionRouterServiceProvider::class,
\Hermes\Providers\HttpBodyParserServiceProvider::class,
\Hermes\Providers\ConsoleSupportServiceProvider::class
\Hermes\Providers\ConsoleSupportServiceProvider::class,
\Hermes\Test\ActionServiceProvider::class

]

Expand Down
126 changes: 126 additions & 0 deletions src/Commands/ActionCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Hermes\Commands;


use Hermes\Core\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Hermes\Core\Contracts\Console\Kernel;
use Hermes\Core\Routing\ActionCollection;

class ActionCacheCommand extends Command
{

/**
* The console command name.
*
* @var string
*/
protected $name = 'action:cache';

/**
* The console command description
*
* @var string
*/
protected $description = 'Create an action cache file for faster action registration.';

/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* Create a new action command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
*/
public function __construct(Filesystem $files)
{

parent::__construct();
$this->files = $files;

}

/**
* Execute the console command.
*/
public function fire()
{

$this->call('action:clear');

$actions = $this->getFreshApplicationActions();

if(count($actions) == 0) {
$this->error("Your application doesn't have any actions.");
return;
}

foreach ($actions as $action)
{

$action->prepareForSerialization();

}

$this->files->put(
$this->hermes->getCachedActionsPath(), $this->buildActionCacheFile($actions)
);

$this->info('Actions cached successfully!');

}

/**
* Boot a fresh copy of the application and get the actions.
*
* @return \Hermes\Core\Routing\ActionCollection
*/
protected function getFreshApplicationActions()
{

return tap($this->getFreshApplication()['hermes.router']->getActions(), function($actions) {

$actions->refreshNameLookups();
$actions->refreshActionLookups();

});

}

/**
* Get a fresh application instance.
*
* @return \Hermes\Core\Application
*/
protected function getFreshApplication()
{

$app = require $this->hermes->bootstrapPath().'/app.php';

return tap($app, function($app) {
$app->make(Kernel::class)->bootstrap();
});

}

/**
* Build the action cache file.
*
* @param \Hermes\Core\Routing\ActionCollection $actions
*
* @return string
*/
protected function buildActionCacheFile(ActionCollection $actions)
{

$stub = $this->files->get(__DIR__.'/stubs/actions.stub');

return str_replace('{{actions}}', base64_encode(serialize($actions)), $stub);
}

}
54 changes: 54 additions & 0 deletions src/Commands/ActionClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Hermes\Commands;

use Hermes\Core\Console\Command;
use Illuminate\Filesystem\Filesystem;

class ActionClearCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'action:clear';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove the action cache file';

/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* Create a new action clear command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct();

$this->files = $files;
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$this->files->delete($this->hermes->getCachedActionsPath());

$this->info('Action cache cleared!');
}
}
16 changes: 16 additions & 0 deletions src/Commands/stubs/actions.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
|--------------------------------------------------------------------------
| Load The Cached Routes
|--------------------------------------------------------------------------
|
| Here we will decode and unserialize the RouteCollection instance that
| holds all of the route information for an application. This allows
| us to instantaneously load the entire route map into the router.
|
*/

app('hermes.router')->setActions(
unserialize(base64_decode('{{actions}}'))
);
26 changes: 24 additions & 2 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,7 @@ public function booted($callback)
protected function fireAppCallbacks(array $callbacks)
{
foreach ($callbacks as $callback) {
dump($callback);
call_user_func($callback, $this);
dump('CALL_OK');
}
}

Expand Down Expand Up @@ -785,4 +783,28 @@ public function isDownForMaintenance()
return false;
}

/**
* Determine if the application actions are cached.
*
* @return bool
*/
public function actionsAreCached()
{

return $this['files']->exists($this->getCachedActionsPath());

}

/**
* Get the path of the actions cache file
*
* @return string
*/
public function getCachedActionsPath()
{

return $this->bootstrapPath() . '/cache/actions.php';

}

}
6 changes: 0 additions & 6 deletions src/Core/Contracts/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: desh
* Date: 04/09/17
* Time: 18.20
*/

namespace Hermes\Core\Contracts\Console;

Expand Down
74 changes: 74 additions & 0 deletions src/Core/Contracts/Routing/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Hermes\Core\Contracts\Routing;


use Hermes\Ink\Action;

interface Router
{

/**
* Register a new GET action with the router.
*
* @param string $uri
* @param \Closure|array|string|null $action
* @return Action
*/
public function get($uri, $action = null);

/**
* Register a new POST action with the router.
*
* @param string $uri
* @param \Closure|array|string|null $action
* @return Action
*/
public function post($uri, $action = null);

/**
* Register a new PUT action with the router.
*
* @param string $uri
* @param \Closure|array|string|null $action
* @return Action
*/
public function put($uri, $action = null);

/**
* Register a new PATCH action with the router.
*
* @param string $uri
* @param \Closure|array|string|null $action
* @return Action
*/
public function patch($uri, $action = null);

/**
* Register a new DELETE action with the router.
*
* @param string $uri
* @param \Closure|array|string|null $action
* @return Action
*/
public function delete($uri, $action = null);

/**
* Register a new OPTIONS action with the router.
*
* @param string $uri
* @param \Closure|array|string|null $action
* @return Action
*/
public function options($uri, $action = null);

/**
* Create an action group with shared attributes.
*
* @param array $attributes
* @param \Closure|string $actions
* @return void
*/
public function group(array $attributes, $actions);

}
2 changes: 1 addition & 1 deletion src/Core/Routing/ActionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ActionCollection implements Countable, IteratorAggregate
*
* @var array
*/
public $nameList = [];
protected $nameList = [];

/**
* A look-up table of actions by class.
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Routing/ActionRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Illuminate\Support\Str;
use Illuminate\Container\Container;
use Illuminate\Support\Traits\Macroable;
use Hermes\Core\Contracts\Routing\Router;

class ActionRouter
class ActionRouter implements Router
{
use Macroable {
__call as macroCall;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/helpers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

if(!function_exists('hermes'))
if(!function_exists('app'))
{

function hermes($abstract = null, array $parameters = [])
function app($abstract = null, array $parameters = [])
{

if(defined('LARAVEL_START')){
Expand Down
Loading

0 comments on commit 2a4756a

Please sign in to comment.