Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -38,7 +38,7 @@ class DumperCollection implements \IteratorAggregate
/**
* Returns the children routes and collections.
*
* @return DumperCollection[]|DumperRoute[] Array of DumperCollection|DumperRoute
* @return self[]|DumperRoute[]
*/
public function all()
{
@@ -86,7 +86,7 @@ class DumperCollection implements \IteratorAggregate
/**
* Returns the root of the collection.
*
* @return DumperCollection The root collection
* @return self The root collection
*/
public function getRoot()
{
@@ -96,7 +96,7 @@ class DumperCollection implements \IteratorAggregate
/**
* Returns the parent collection.
*
* @return DumperCollection|null The parent collection or null if the collection has no parent
* @return self|null The parent collection or null if the collection has no parent
*/
protected function getParent()
{
@@ -105,10 +105,8 @@ class DumperCollection implements \IteratorAggregate
/**
* Sets the parent collection.
*
* @param DumperCollection $parent The parent collection
*/
protected function setParent(DumperCollection $parent)
protected function setParent(self $parent)
{
$this->parent = $parent;
}

View File

@@ -1,107 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Matcher\Dumper;
/**
* Prefix tree of routes preserving routes order.
*
* @author Arnaud Le Blanc <arnaud.lb@gmail.com>
*
* @internal
*/
class DumperPrefixCollection extends DumperCollection
{
/**
* @var string
*/
private $prefix = '';
/**
* Returns the prefix.
*
* @return string The prefix
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Sets the prefix.
*
* @param string $prefix The prefix
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Adds a route in the tree.
*
* @param DumperRoute $route The route
*
* @return DumperPrefixCollection The node the route was added to
*
* @throws \LogicException
*/
public function addPrefixRoute(DumperRoute $route)
{
$prefix = $route->getRoute()->compile()->getStaticPrefix();
for ($collection = $this; null !== $collection; $collection = $collection->getParent()) {
// Same prefix, add to current leave
if ($collection->prefix === $prefix) {
$collection->add($route);
return $collection;
}
// Prefix starts with route's prefix
if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) {
$child = new self();
$child->setPrefix(substr($prefix, 0, strlen($collection->prefix) + 1));
$collection->add($child);
return $child->addPrefixRoute($route);
}
}
// Reached only if the root has a non empty prefix
throw new \LogicException('The collection root must not have a prefix');
}
/**
* Merges nodes whose prefix ends with a slash.
*
* Children of a node whose prefix ends with a slash are moved to the parent node
*/
public function mergeSlashNodes()
{
$children = array();
foreach ($this as $child) {
if ($child instanceof self) {
$child->mergeSlashNodes();
if ('/' === substr($child->prefix, -1)) {
$children = array_merge($children, $child->all());
} else {
$children[] = $child;
}
} else {
$children[] = $child;
}
}
$this->setAll($children);
}
}

View File

@@ -22,19 +22,10 @@ use Symfony\Component\Routing\Route;
*/
class DumperRoute
{
/**
* @var string
*/
private $name;
/**
* @var Route
*/
private $route;
/**
* Constructor.
*
* @param string $name The route name
* @param Route $route The route
*/

View File

@@ -20,16 +20,8 @@ use Symfony\Component\Routing\RouteCollection;
*/
abstract class MatcherDumper implements MatcherDumperInterface
{
/**
* @var RouteCollection
*/
private $routes;
/**
* Constructor.
*
* @param RouteCollection $routes The RouteCollection to dump
*/
public function __construct(RouteCollection $routes)
{
$this->routes = $routes;

View File

@@ -11,10 +11,10 @@
namespace Symfony\Component\Routing\Matcher\Dumper;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
/**
* PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.
@@ -63,16 +63,11 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
* {$options['class']}.
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class {$options['class']} extends {$options['base_class']}
{
/**
* Constructor.
*/
public function __construct(RequestContext \$context)
{
\$this->context = \$context;
@@ -101,12 +96,18 @@ EOF;
$code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n");
return <<<EOF
public function match(\$pathinfo)
public function match(\$rawPathinfo)
{
\$allow = array();
\$pathinfo = rawurldecode(\$pathinfo);
\$pathinfo = rawurldecode(\$rawPathinfo);
\$trimmedPathinfo = rtrim(\$pathinfo, '/');
\$context = \$this->context;
\$request = \$this->request;
\$request = \$this->request ?: \$this->createRequest(\$pathinfo);
\$requestMethod = \$canonicalMethod = \$context->getMethod();
if ('HEAD' === \$requestMethod) {
\$canonicalMethod = 'GET';
}
$code
@@ -126,22 +127,21 @@ EOF;
private function compileRoutes(RouteCollection $routes, $supportsRedirections)
{
$fetchedHost = false;
$groups = $this->groupRoutesByHostRegex($routes);
$code = '';
foreach ($groups as $collection) {
if (null !== $regex = $collection->getAttribute('host_regex')) {
if (!$fetchedHost) {
$code .= " \$host = \$this->context->getHost();\n\n";
$code .= " \$host = \$context->getHost();\n\n";
$fetchedHost = true;
}
$code .= sprintf(" if (preg_match(%s, \$host, \$hostMatches)) {\n", var_export($regex, true));
}
$tree = $this->buildPrefixTree($collection);
$groupCode = $this->compilePrefixRoutes($tree, $supportsRedirections);
$tree = $this->buildStaticPrefixCollection($collection);
$groupCode = $this->compileStaticPrefixRoutes($tree, $supportsRedirections);
if (null !== $regex) {
// apply extra indention at each line (except empty ones)
@@ -153,40 +153,59 @@ EOF;
}
}
// used to display the Welcome Page in apps that don't define a homepage
$code .= " if ('/' === \$pathinfo && !\$allow) {\n";
$code .= " throw new Symfony\Component\Routing\Exception\NoConfigurationException();\n";
$code .= " }\n";
return $code;
}
private function buildStaticPrefixCollection(DumperCollection $collection)
{
$prefixCollection = new StaticPrefixCollection();
foreach ($collection as $dumperRoute) {
$prefix = $dumperRoute->getRoute()->compile()->getStaticPrefix();
$prefixCollection->addRoute($prefix, $dumperRoute);
}
$prefixCollection->optimizeGroups();
return $prefixCollection;
}
/**
* Generates PHP code recursively to match a tree of routes.
* Generates PHP code to match a tree of routes.
*
* @param DumperPrefixCollection $collection A DumperPrefixCollection instance
* @param StaticPrefixCollection $collection A StaticPrefixCollection instance
* @param bool $supportsRedirections Whether redirections are supported by the base class
* @param string $parentPrefix Prefix of the parent collection
* @param string $ifOrElseIf either "if" or "elseif" to influence chaining
*
* @return string PHP code
*/
private function compilePrefixRoutes(DumperPrefixCollection $collection, $supportsRedirections, $parentPrefix = '')
private function compileStaticPrefixRoutes(StaticPrefixCollection $collection, $supportsRedirections, $ifOrElseIf = 'if')
{
$code = '';
$prefix = $collection->getPrefix();
$optimizable = 1 < strlen($prefix) && 1 < count($collection->all());
$optimizedPrefix = $parentPrefix;
if ($optimizable) {
$optimizedPrefix = $prefix;
$code .= sprintf(" if (0 === strpos(\$pathinfo, %s)) {\n", var_export($prefix, true));
if (!empty($prefix) && '/' !== $prefix) {
$code .= sprintf(" %s (0 === strpos(\$pathinfo, %s)) {\n", $ifOrElseIf, var_export($prefix, true));
}
foreach ($collection as $route) {
if ($route instanceof DumperCollection) {
$code .= $this->compilePrefixRoutes($route, $supportsRedirections, $optimizedPrefix);
$ifOrElseIf = 'if';
foreach ($collection->getItems() as $route) {
if ($route instanceof StaticPrefixCollection) {
$code .= $this->compileStaticPrefixRoutes($route, $supportsRedirections, $ifOrElseIf);
$ifOrElseIf = 'elseif';
} else {
$code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix)."\n";
$code .= $this->compileRoute($route[1]->getRoute(), $route[1]->getName(), $supportsRedirections, $prefix)."\n";
$ifOrElseIf = 'if';
}
}
if ($optimizable) {
if (!empty($prefix) && '/' !== $prefix) {
$code .= " }\n\n";
// apply extra indention at each line (except empty ones)
$code = preg_replace('/^.{2,}$/m', ' $0', $code);
@@ -217,26 +236,21 @@ EOF;
$hostMatches = false;
$methods = $route->getMethods();
// GET and HEAD are equivalent
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
$methods[] = 'HEAD';
}
$supportsTrailingSlash = $supportsRedirections && (!$methods || \in_array('GET', $methods));
$regex = $compiledRoute->getRegex();
$supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('HEAD', $methods));
if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
if ($supportsTrailingSlash && substr($m['url'], -1) === '/') {
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
if (!\count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.('u' === substr($regex, -1) ? 'u' : ''), $regex, $m)) {
if ($supportsTrailingSlash && '/' === substr($m['url'], -1)) {
$conditions[] = sprintf('%s === $trimmedPathinfo', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
$hasTrailingSlash = true;
} else {
$conditions[] = sprintf('$pathinfo === %s', var_export(str_replace('\\', '', $m['url']), true));
$conditions[] = sprintf('%s === $pathinfo', var_export(str_replace('\\', '', $m['url']), true));
}
} else {
if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() !== $parentPrefix) {
$conditions[] = sprintf('0 === strpos($pathinfo, %s)', var_export($compiledRoute->getStaticPrefix(), true));
}
$regex = $compiledRoute->getRegex();
if ($supportsTrailingSlash && $pos = strpos($regex, '/$')) {
$regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
$hasTrailingSlash = true;
@@ -263,53 +277,9 @@ EOF;
EOF;
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
if ($methods) {
if (1 === count($methods)) {
$code .= <<<EOF
if (\$this->context->getMethod() != '$methods[0]') {
\$allow[] = '$methods[0]';
goto $gotoname;
}
EOF;
} else {
$methods = implode("', '", $methods);
$code .= <<<EOF
if (!in_array(\$this->context->getMethod(), array('$methods'))) {
\$allow = array_merge(\$allow, array('$methods'));
goto $gotoname;
}
EOF;
}
}
if ($hasTrailingSlash) {
$code .= <<<EOF
if (substr(\$pathinfo, -1) !== '/') {
return \$this->redirect(\$pathinfo.'/', '$name');
}
EOF;
}
if ($schemes = $route->getSchemes()) {
if (!$supportsRedirections) {
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes));
}
EOF;
}
// the offset where the return value is appended below, with indendation
$retOffset = 12 + \strlen($code);
// optimize parameters array
if ($matches || $hostMatches) {
@@ -323,18 +293,93 @@ EOF;
$vars[] = "array('_route' => '$name')";
$code .= sprintf(
" return \$this->mergeDefaults(array_replace(%s), %s);\n",
" \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n",
implode(', ', $vars),
str_replace("\n", '', var_export($route->getDefaults(), true))
);
} elseif ($route->getDefaults()) {
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
$code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
} else {
$code .= sprintf(" return array('_route' => '%s');\n", $name);
$code .= sprintf(" \$ret = array('_route' => '%s');\n", $name);
}
if ($hasTrailingSlash) {
$code .= <<<EOF
if ('/' === substr(\$pathinfo, -1)) {
// no-op
} elseif ('GET' !== \$canonicalMethod) {
goto $gotoname;
} else {
return array_replace(\$ret, \$this->redirect(\$rawPathinfo.'/', '$name'));
}
EOF;
}
if ($methods) {
$methodVariable = \in_array('GET', $methods) ? '$canonicalMethod' : '$requestMethod';
$methods = implode("', '", $methods);
}
if ($schemes = $route->getSchemes()) {
if (!$supportsRedirections) {
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
if ($methods) {
$code .= <<<EOF
\$requiredSchemes = $schemes;
\$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]);
if (!in_array($methodVariable, array('$methods'))) {
if (\$hasRequiredScheme) {
\$allow = array_merge(\$allow, array('$methods'));
}
goto $gotoname;
}
if (!\$hasRequiredScheme) {
if ('GET' !== \$canonicalMethod) {
goto $gotoname;
}
return array_replace(\$ret, \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes)));
}
EOF;
} else {
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$context->getScheme()])) {
if ('GET' !== \$canonicalMethod) {
goto $gotoname;
}
return array_replace(\$ret, \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes)));
}
EOF;
}
} elseif ($methods) {
$code .= <<<EOF
if (!in_array($methodVariable, array('$methods'))) {
\$allow = array_merge(\$allow, array('$methods'));
goto $gotoname;
}
EOF;
}
if ($hasTrailingSlash || $schemes || $methods) {
$code .= " return \$ret;\n";
} else {
$code = substr_replace($code, 'return', $retOffset, 6);
}
$code .= " }\n";
if ($methods) {
if ($hasTrailingSlash || $schemes || $methods) {
$code .= " $gotoname:\n";
}
@@ -353,7 +398,6 @@ EOF;
private function groupRoutesByHostRegex(RouteCollection $routes)
{
$groups = new DumperCollection();
$currentGroup = new DumperCollection();
$currentGroup->setAttribute('host_regex', null);
$groups->add($currentGroup);
@@ -371,30 +415,6 @@ EOF;
return $groups;
}
/**
* Organizes the routes into a prefix tree.
*
* Routes order is preserved such that traversing the tree will traverse the
* routes in the origin order.
*
* @param DumperCollection $collection A collection of routes
*
* @return DumperPrefixCollection
*/
private function buildPrefixTree(DumperCollection $collection)
{
$tree = new DumperPrefixCollection();
$current = $tree;
foreach ($collection as $route) {
$current = $current->addPrefixRoute($route);
}
$tree->mergeSlashNodes();
return $tree;
}
private function getExpressionLanguage()
{
if (null === $this->expressionLanguage) {

View File

@@ -0,0 +1,238 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Matcher\Dumper;
/**
* Prefix tree of routes preserving routes order.
*
* @author Frank de Jonge <info@frankdejonge.nl>
*
* @internal
*/
class StaticPrefixCollection
{
/**
* @var string
*/
private $prefix;
/**
* @var array[]|StaticPrefixCollection[]
*/
private $items = array();
/**
* @var int
*/
private $matchStart = 0;
public function __construct($prefix = '')
{
$this->prefix = $prefix;
}
public function getPrefix()
{
return $this->prefix;
}
/**
* @return mixed[]|StaticPrefixCollection[]
*/
public function getItems()
{
return $this->items;
}
/**
* Adds a route to a group.
*
* @param string $prefix
* @param mixed $route
*/
public function addRoute($prefix, $route)
{
$prefix = '/' === $prefix ? $prefix : rtrim($prefix, '/');
$this->guardAgainstAddingNotAcceptedRoutes($prefix);
if ($this->prefix === $prefix) {
// When a prefix is exactly the same as the base we move up the match start position.
// This is needed because otherwise routes that come afterwards have higher precedence
// than a possible regular expression, which goes against the input order sorting.
$this->items[] = array($prefix, $route);
$this->matchStart = \count($this->items);
return;
}
foreach ($this->items as $i => $item) {
if ($i < $this->matchStart) {
continue;
}
if ($item instanceof self && $item->accepts($prefix)) {
$item->addRoute($prefix, $route);
return;
}
$group = $this->groupWithItem($item, $prefix, $route);
if ($group instanceof self) {
$this->items[$i] = $group;
return;
}
}
// No optimised case was found, in this case we simple add the route for possible
// grouping when new routes are added.
$this->items[] = array($prefix, $route);
}
/**
* Tries to combine a route with another route or group.
*
* @param StaticPrefixCollection|array $item
* @param string $prefix
* @param mixed $route
*
* @return null|StaticPrefixCollection
*/
private function groupWithItem($item, $prefix, $route)
{
$itemPrefix = $item instanceof self ? $item->prefix : $item[0];
$commonPrefix = $this->detectCommonPrefix($prefix, $itemPrefix);
if (!$commonPrefix) {
return;
}
$child = new self($commonPrefix);
if ($item instanceof self) {
$child->items = array($item);
} else {
$child->addRoute($item[0], $item[1]);
}
$child->addRoute($prefix, $route);
return $child;
}
/**
* Checks whether a prefix can be contained within the group.
*
* @param string $prefix
*
* @return bool Whether a prefix could belong in a given group
*/
private function accepts($prefix)
{
return '' === $this->prefix || 0 === strpos($prefix, $this->prefix);
}
/**
* Detects whether there's a common prefix relative to the group prefix and returns it.
*
* @param string $prefix
* @param string $anotherPrefix
*
* @return false|string A common prefix, longer than the base/group prefix, or false when none available
*/
private function detectCommonPrefix($prefix, $anotherPrefix)
{
$baseLength = \strlen($this->prefix);
$commonLength = $baseLength;
$end = min(\strlen($prefix), \strlen($anotherPrefix));
for ($i = $baseLength; $i <= $end; ++$i) {
if (substr($prefix, 0, $i) !== substr($anotherPrefix, 0, $i)) {
break;
}
$commonLength = $i;
}
$commonPrefix = rtrim(substr($prefix, 0, $commonLength), '/');
if (\strlen($commonPrefix) > $baseLength) {
return $commonPrefix;
}
return false;
}
/**
* Optimizes the tree by inlining items from groups with less than 3 items.
*/
public function optimizeGroups()
{
$index = -1;
while (isset($this->items[++$index])) {
$item = $this->items[$index];
if ($item instanceof self) {
$item->optimizeGroups();
// When a group contains only two items there's no reason to optimize because at minimum
// the amount of prefix check is 2. In this case inline the group.
if ($item->shouldBeInlined()) {
array_splice($this->items, $index, 1, $item->items);
// Lower index to pass through the same index again after optimizing.
// The first item of the replacements might be a group needing optimization.
--$index;
}
}
}
}
private function shouldBeInlined()
{
if (\count($this->items) >= 3) {
return false;
}
foreach ($this->items as $item) {
if ($item instanceof self) {
return true;
}
}
foreach ($this->items as $item) {
if (\is_array($item) && $item[0] === $this->prefix) {
return false;
}
}
return true;
}
/**
* Guards against adding incompatible prefixes in a group.
*
* @param string $prefix
*
* @throws \LogicException when a prefix does not belong in a group
*/
private function guardAgainstAddingNotAcceptedRoutes($prefix)
{
if (!$this->accepts($prefix)) {
$message = sprintf('Could not add route with prefix %s to collection with prefix %s', $prefix, $this->prefix);
throw new \LogicException($message);
}
}
}