Skip to content

Commit

Permalink
Extract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jan 21, 2020
1 parent 0c4b7d4 commit 9cb7657
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/Illuminate/Foundation/Console/RouteCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Routing\RouteCollection;
use Illuminate\Support\Str;
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;

class RouteCacheCommand extends Command
{
Expand Down Expand Up @@ -61,24 +58,11 @@ public function handle()
return $this->error("Your application doesn't have any routes.");
}

$symfonyRoutes = new SymfonyRouteCollection();

foreach ($routes as &$route) {
foreach ($routes as $route) {
$route->prepareForSerialization();

// If the route doesn't have a name, we'll generate one for it
// and re-add the route to the collection. This way we can
// add the route to the Symfony route collection.
if (! $name = $route->getName()) {
$route->name($name = Str::random());

$routes->add($route);
}

$symfonyRoutes->add($name, $route->toSymfonyRoute());
}

$compiled = (new CompiledUrlMatcherDumper($symfonyRoutes))->getCompiledRoutes();
$compiled = $routes->dumper()->getCompiledRoutes();

$this->files->put(
$this->laravel->getCachedRoutesPath(),
Expand Down
40 changes: 40 additions & 0 deletions src/Illuminate/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use IteratorAggregate;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;

class RouteCollection implements Countable, IteratorAggregate
{
Expand Down Expand Up @@ -396,4 +399,41 @@ public function count()
{
return count($this->getRoutes());
}

/**
* Return the CompiledUrlMatcherDumper instance for the route collection.
*
* @return \Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper
*/
public function dumper()
{
return new CompiledUrlMatcherDumper($this->toSymfonyRouteCollection());
}

/**
* Convert the collection to a Symfony RouteCollection instance.
*
* @return \Symfony\Component\Routing\RouteCollection
*/
public function toSymfonyRouteCollection()
{
$symfonyRoutes = new SymfonyRouteCollection();

foreach ($this->getRoutes() as $route) {
// If the route doesn't have a name, we'll generate one for it
// and re-add the route to the collection. This way we can
// add the route to the Symfony route collection.
if (! $name = $route->getName()) {
$route->name($name = Str::random());

$this->add($route);
}

$symfonyRoutes->add($name, $route->toSymfonyRoute());
}

$this->refreshNameLookups();

return $symfonyRoutes;
}
}

0 comments on commit 9cb7657

Please sign in to comment.