本文整理汇总了PHP中Symfony\Component\Routing\Route::compile方法的典型用法代码示例。如果您正苦于以下问题:PHP Route::compile方法的具体用法?PHP Route::compile怎么用?PHP Route::compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Routing\Route
的用法示例。
在下文中一共展示了Route::compile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: describeRoute
/**
* {@inheritdoc}
*/
protected function describeRoute(Route $route, array $options = array())
{
$tableHeaders = array('Property', 'Value');
$tableRows = array(array('Route Name', $options['name']), array('Path', $route->getPath()), array('Path Regex', $route->compile()->getRegex()), array('Host', '' !== $route->getHost() ? $route->getHost() : 'ANY'), array('Host Regex', '' !== $route->getHost() ? $route->compile()->getHostRegex() : ''), array('Scheme', $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'), array('Method', $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'), array('Requirements', $route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM'), array('Class', get_class($route)), array('Defaults', $this->formatRouterConfig($route->getDefaults())), array('Options', $this->formatRouterConfig($route->getOptions())));
$table = new Table($this->getOutput());
$table->setHeaders($tableHeaders)->setRows($tableRows);
$table->render();
}
示例2: describeRoute
/**
* {@inheritdoc}
*/
protected function describeRoute(Route $route, array $options = array())
{
$requirements = $route->getRequirements();
unset($requirements['_scheme'], $requirements['_method']);
$output = '- Path: ' . $route->getPath() . "\n" . '- Path Regex: ' . $route->compile()->getRegex() . "\n" . '- Host: ' . ('' !== $route->getHost() ? $route->getHost() : 'ANY') . "\n" . '- Host Regex: ' . ('' !== $route->getHost() ? $route->compile()->getHostRegex() : '') . "\n" . '- Scheme: ' . ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY') . "\n" . '- Method: ' . ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY') . "\n" . '- Class: ' . get_class($route) . "\n" . '- Defaults: ' . $this->formatRouterConfig($route->getDefaults()) . "\n" . '- Requirements: ' . ($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM') . "\n" . '- Options: ' . $this->formatRouterConfig($route->getOptions());
$this->write(isset($options['name']) ? $options['name'] . "\n" . str_repeat('-', strlen($options['name'])) . "\n\n" . $output : $output);
$this->write("\n");
}
示例3: describeRoute
/**
* {@inheritdoc}
*/
protected function describeRoute(Route $route, array $options = array())
{
// fixme: values were originally written as raw
$description = array('<comment>Path</comment> ' . $route->getPath(), '<comment>Path Regex</comment> ' . $route->compile()->getRegex(), '<comment>Host</comment> ' . ('' !== $route->getHost() ? $route->getHost() : 'ANY'), '<comment>Host Regex</comment> ' . ('' !== $route->getHost() ? $route->compile()->getHostRegex() : ''), '<comment>Scheme</comment> ' . ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'), '<comment>Method</comment> ' . ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'), '<comment>Class</comment> ' . get_class($route), '<comment>Defaults</comment> ' . $this->formatRouterConfig($route->getDefaults()), '<comment>Requirements</comment> ' . ($route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM'), '<comment>Options</comment> ' . $this->formatRouterConfig($route->getOptions()));
if (isset($options['name'])) {
array_unshift($description, '<comment>Name</comment> ' . $options['name']);
array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
}
$this->writeText(implode("\n", $description) . "\n", $options);
}
示例4: createFromRoute
public function createFromRoute(Route $route)
{
$compiledRoute = $route->compile();
$defaults = array_intersect_key($route->getDefaults(), array_fill_keys($compiledRoute->getVariables(), null));
$tokens = $compiledRoute->getTokens();
return new ExtractedRoute($tokens, $defaults);
}
示例5: matchCollection
protected function matchCollection($pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
// does it match without any requirements?
$r = new Route($route->getPath(), $route->getDefaults(), array(), $route->getOptions());
$cr = $r->compile();
if (!preg_match($cr->getRegex(), $pathinfo)) {
$this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
continue;
}
foreach ($route->getRequirements() as $n => $regex) {
$r = new Route($route->getPath(), $route->getDefaults(), array($n => $regex), $route->getOptions());
$cr = $r->compile();
if (in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
$this->addTrace(sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue 2;
}
}
continue;
}
// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === $method = $this->context->getMethod()) {
$method = 'GET';
}
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
$this->allow = array_merge($this->allow, $req);
$this->addTrace(sprintf('Method "%s" does not match the requirement ("%s")', $this->context->getMethod(), implode(', ', $req)), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
}
// check HTTP scheme requirement
if ($scheme = $route->getRequirement('_scheme')) {
if ($this->context->getScheme() !== $scheme) {
$this->addTrace(sprintf('Scheme "%s" does not match the requirement ("%s"); the user will be redirected', $this->context->getScheme(), $scheme), self::ROUTE_ALMOST_MATCHES, $name, $route);
return true;
}
}
$this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
return true;
}
}
示例6: matchCollection
protected function matchCollection($pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
// does it match without any requirements?
$r = new Route($route->getPath(), $route->getDefaults(), array(), $route->getOptions());
$cr = $r->compile();
if (!preg_match($cr->getRegex(), $pathinfo)) {
$this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
continue;
}
foreach ($route->getRequirements() as $n => $regex) {
$r = new Route($route->getPath(), $route->getDefaults(), array($n => $regex), $route->getOptions());
$cr = $r->compile();
if (in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
$this->addTrace(sprintf('Requirement for "%s" does not match (%s)', $n, $regex), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue 2;
}
}
continue;
}
// check host requirement
$hostMatches = array();
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
$this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === ($method = $this->context->getMethod())) {
$method = 'GET';
}
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
$this->allow = array_merge($this->allow, $req);
$this->addTrace(sprintf('Method "%s" does not match the requirement ("%s")', $this->context->getMethod(), implode(', ', $req)), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
}
// check condition
if ($condition = $route->getCondition()) {
if (!$this->getExpressionLanguage()->evaluate($condition, array('context' => $this->context, 'request' => $this->request))) {
$this->addTrace(sprintf('Condition "%s" does not evaluate to "true"', $condition), self::ROUTE_ALMOST_MATCHES, $name, $route);
continue;
}
}
// check HTTP scheme requirement
if ($requiredSchemes = $route->getSchemes()) {
$scheme = $this->context->getScheme();
if (!$route->hasScheme($scheme)) {
$this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes ("%s"); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route);
return true;
}
}
$this->addTrace('Route matches!', self::ROUTE_MATCHES, $name, $route);
return true;
}
}
示例7: getParameters
/**
* @param Route $route
* @return Generator
*/
private function getParameters(Route $route) : Generator
{
/** @var CompiledRoute $compiled */
$compiled = $route->compile();
foreach ($compiled->getVariables() as $name) {
(yield ['name' => $name, 'type' => 'string', 'required' => true, 'in' => 'path']);
}
}
示例8: handle
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
{
// description
if (null === $annotation->getDescription()) {
$comments = explode("\n", $annotation->getDocumentation());
// just set the first line
$comment = trim($comments[0]);
$comment = preg_replace("#\n+#", ' ', $comment);
$comment = preg_replace('#\\s+#', ' ', $comment);
$comment = preg_replace('#[_`*]+#', '', $comment);
if ('@' !== substr($comment, 0, 1)) {
$annotation->setDescription($comment);
}
}
// requirements
$requirements = $annotation->getRequirements();
foreach ($route->getRequirements() as $name => $value) {
if (!isset($requirements[$name]) && '_method' !== $name) {
$requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
}
if ('_scheme' == $name) {
$https = 'https' == $value;
$annotation->setHttps($https);
}
}
$paramDocs = array();
foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
if (preg_match('{^@param (.+)}', trim($line), $matches)) {
$paramDocs[] = $matches[1];
}
if (preg_match('{^@deprecated\\b(.*)}', trim($line), $matches)) {
$annotation->setDeprecated(true);
}
if (preg_match('{^@link\\b(.*)}', trim($line), $matches)) {
$annotation->setLink($matches[1]);
}
}
$regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
foreach ($route->compile()->getVariables() as $var) {
$found = false;
foreach ($paramDocs as $paramDoc) {
if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
$requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
$requirements[$var]['description'] = $matches[2];
if (!isset($requirements[$var]['requirement'])) {
$requirements[$var]['requirement'] = '';
}
$found = true;
break;
}
}
if (!isset($requirements[$var]) && false === $found) {
$requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
}
}
$annotation->setRequirements($requirements);
}
示例9: testCompilationDefaultValue
/**
* Confirms that a compiled route with default values has the correct outline.
*/
public function testCompilationDefaultValue()
{
// Because "here" has a default value, it should not factor into the outline
// or the fitness.
$route = new Route('/test/{something}/more/{here}', array('here' => 'there'));
$route->setOption('compiler_class', 'Drupal\\Core\\Routing\\RouteCompiler');
$compiled = $route->compile();
$this->assertEquals($compiled->getFit(), 5, 'The fit was not correct.');
$this->assertEquals($compiled->getPatternOutline(), '/test/%/more', 'The pattern outline was not correct.');
}
示例10: applies
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route)
{
if (!empty($definition['type']) && strpos($definition['type'], 'entity_revision:') === 0) {
$entity_type_id = substr($definition['type'], strlen('entity:'));
if (strpos($definition['type'], '{') !== FALSE) {
$entity_type_slug = substr($entity_type_id, 1, -1);
return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
}
return $this->entityManager->hasDefinition($entity_type_id);
}
return FALSE;
}
示例11: compile
public function compile()
{
$defaults = $this->getDefaults();
if (array_key_exists('_formats', $defaults)) {
$path = $this->getPath();
// add format to the path
$this->setPath($path . ".{_format}");
// add a requirement
$this->addRequirements(['_format' => implode('|', $defaults['_formats'])]);
}
return parent::compile();
}
示例12: getDataFromRoute
/**
* get collection and id from route
*
* @param Route $route route to look at
* @param string $value value of reference as URI
*
* @return array
*/
private function getDataFromRoute(Route $route, $value)
{
if ($route->getRequirement('id') !== null && $route->getMethods() === ['GET'] && preg_match($route->compile()->getRegex(), $value, $matches)) {
$id = $matches['id'];
list($routeService) = explode(':', $route->getDefault('_controller'));
list($core, $bundle, , $name) = explode('.', $routeService);
$serviceName = implode('.', [$core, $bundle, 'rest', $name]);
$collection = array_search($serviceName, $this->mapping);
return [$collection, $id];
}
return [null, null];
}
示例13: applies
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route)
{
if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
$entity_type_id = substr($definition['type'], strlen('entity:'));
if (strpos($definition['type'], '{') !== FALSE) {
$entity_type_slug = substr($entity_type_id, 1, -1);
return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
}
// This converter only applies rdf entities.
$entity_storage = $this->entityManager->getStorage($entity_type_id);
if ($entity_storage instanceof RdfEntitySparqlStorage) {
return $this->entityManager->hasDefinition($entity_type_id);
}
}
return FALSE;
}
示例14: extractRawAttributes
/**
* Extracts all of the raw attributes from a path for a given route.
*
* @param \Symfony\Component\Routing\Route $route
* The route object.
* @param string $name
* The route name.
* @param string $path
* A path.
*
* @return array
* An array of raw attributes for this path and route.
*/
public static function extractRawAttributes(Route $route, $name, $path)
{
// See \Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection().
preg_match($route->compile()->getRegex(), $path, $matches);
// See \Symfony\Component\Routing\Matcher\UrlMatcher::mergeDefaults().
$attributes = $route->getDefaults();
foreach ($matches as $key => $value) {
if (!is_int($key)) {
$attributes[$key] = $value;
}
}
// See \Symfony\Cmf\Component\Routing\NestedMatcher\UrlMatcher::getAttributes().
$attributes[RouteObjectInterface::ROUTE_OBJECT] = $route;
$attributes[RouteObjectInterface::ROUTE_NAME] = $name;
return $attributes;
}
示例15: matchRoute
/**
* Tries to match a URL with an individual route.
*
* @param $pathinfo
* @param $name
* @param BaseRoute $route
* @return array|null
*/
protected function matchRoute($pathinfo, $name, BaseRoute $route)
{
$compiledRoute = $route->compile();
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) {
return null;
}
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
return null;
}
$hostMatches = array();
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
return null;
}
// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === ($method = $this->context->getMethod())) {
$method = 'GET';
}
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
$this->allow = array_merge($this->allow, $req);
return null;
}
}
$status = $this->handleRouteRequirements($pathinfo, $name, $route);
if (self::ROUTE_MATCH === $status[0]) {
return $status[1];
}
if (self::REQUIREMENT_MISMATCH === $status[0]) {
return null;
}
$attrs = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
if ($route instanceof Route) {
foreach ($route->getMatchCallbacks() as $callback) {
$ret = call_user_func($callback, $attrs);
if ($ret === false) {
return null;
}
if (is_array($ret)) {
$attrs = $ret;
}
}
}
return $attrs;
}