Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Thumbs.db
.idea/
.phpunit.result.cache
.direnv
.envrc
.envrc
.devenv
105 changes: 105 additions & 0 deletions src/Illuminate/CachedRouting/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php namespace Illuminate\CachedRouting;

/**
* forked from: github.com/MaartenStaa/laravel-41-route-caching
* Copyright (c) 2015 by Maarten Staa.
*
* Some rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * The names of the contributors may not be used to endorse or
* promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

use Illuminate\Routing\Route as LaravelRoute;

class Route extends LaravelRoute
{
/**
* Whether routes should be compiled. Laravel version dependent.
*
* @var null|bool
*/
public static $shouldCompileRoute = null;

/**
* Run the route action and return the response.
*
* @return mixed
*/
#[\Override]
public function run()
{
$app = app();

if ($app['router']->routingToController($this->action) === true) {
$this->action = $app['router']->makeControllerActionClosure($this->action);
}

return parent::run();
}

/**
* Determines whether routes should be compiled. Caches the value at a class
* level.
*
* @return bool
*/
protected function shouldCompile()
{
if (static::$shouldCompileRoute === null) {
// If the compiled variable exists, we should compile.
static::$shouldCompileRoute = array_key_exists('compiled', get_object_vars($this));
}

return static::$shouldCompileRoute;
}

/**
* Compile the route into a Symfony CompiledRoute instance.
*/
#[\Override]
protected function compileRoute()
{
if ($this->shouldCompile() === true && $this->compiled === null) {
parent::compileRoute();
}
}

/**
* Prepare to go to sleep. Compiles the route so that doesn't have to happen
* on every subsequent request.
*
* @return array<string>
*/
public function __sleep()
{
$this->compileRoute();

return array_keys(get_object_vars($this));
}
}
174 changes: 174 additions & 0 deletions src/Illuminate/CachedRouting/RouteCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php namespace Illuminate\CachedRouting;

/**
* forked from: github.com/MaartenStaa/laravel-41-route-caching
* Copyright (c) 2015 by Maarten Staa.
*
* Some rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * The names of the contributors may not be used to endorse or
* promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

use Illuminate\Routing\RouteCollection as LaravelRouteCollection;

class RouteCollection extends LaravelRouteCollection
{
/**
* A backup of the routes we had before clearing the collection in order to
* cache a part of the complete list of routes. The list of routes is put into
* the backup, new routes are defined and cached, and the backup is added back.
*
* @var array
*/
protected $backup;

/**
* Get the backed up routes.
*
* @return array
*/
public function getBackup()
{
return $this->backup;
}

/**
* Save the current collection of routes to the backup to start with an empty
* collection to cache.
*/
public function saveRouteCollection()
{
$this->backup = $this->getCacheableRouteContents();

foreach (array_keys($this->backup) as $k) {
$this->$k = array();
}
}

/**
* Restore the backed up routes.
*/
public function restoreRouteCollection()
{
$this->restoreRoutes($this->getBackup(), true);
}

/**
* Get the routes in a form that can be saved to the cache.
*
* @return string
*/
public function getCacheableRoutes()
{
return serialize($this->getCacheableRouteContents());
}

/**
* Get the data that should be sent to the cache.
*
* @return array
*/
public function getCacheableRouteContents()
{
return array_except(get_object_vars($this), array('backup'));
}

/**
* Restore a set of cached routes into this collection.
*
* @param string $cache
*/
public function restoreRouteCache($cache)
{
$routes = unserialize($cache);

$this->restoreRoutes($routes);
}

/**
* Add a set of routes back into this collection.
*
* @param bool $prepend
* @param array $routes
*/
protected function restoreRoutes($routes, $prepend = false)
{
foreach ($routes as $k => $v) {
if ($k === 'routes') {
$this->$k = $prepend === false ?
$this->mergeGroupedRoutes($this->$k, $v) :
$this->mergeGroupedRoutes($v, $this->$k);
} else {
$this->$k = $prepend === false ?
$this->mergeRoutes($this->$k, $v) :
$this->mergeRoutes($v, $this->$k);
}
}
}

/**
* Merge two array, each containing routes grouped by method.
*
* @param array
* @param array
* @return array
*/
public function mergeGroupedRoutes(array $r1, array $r2)
{
$methods = array('GET', 'POST', 'HEAD', 'PATH', 'PUT', 'DELETE');
foreach ($methods as $method) {
if (isset($r2[$method]) === false) {
continue;
}
if (isset($r1[$method]) === false) {
$r1[$method] = array();
}

$r1[$method] = $this->mergeRoutes($r1[$method], $r2[$method]);
}

return $r1;
}

/**
* Merge two arrays, each containing routes.
*
* @param array
* @param array
* @return array
*/
public function mergeRoutes(array $r1, array $r2)
{
foreach ($r2 as $uri => $route) {
$r1[$uri] = $route;
}

return $r1;
}
}
Loading
Loading