本文整理汇总了PHP中Symfony\Component\Routing\Route::getRequirements方法的典型用法代码示例。如果您正苦于以下问题:PHP Route::getRequirements方法的具体用法?PHP Route::getRequirements怎么用?PHP Route::getRequirements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Routing\Route
的用法示例。
在下文中一共展示了Route::getRequirements方法的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: 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 && '_scheme' !== $name) {
$requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
}
if ('_scheme' === $name) {
$https = 'https' == $value;
$annotation->setHttps($https);
}
}
if (method_exists($route, 'getSchemes')) {
$annotation->setHttps(in_array('https', $route->getSchemes()));
}
$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);
}
示例3: describeRoute
/**
* {@inheritdoc}
*/
protected function describeRoute(Route $route, array $options = array())
{
$requirements = $route->getRequirements();
unset($requirements['_scheme'], $requirements['_method']);
// fixme: values were originally written as raw
$description = array(
'<comment>Path</comment> '.$route->getPath(),
'<comment>Host</comment> '.('' !== $route->getHost() ? $route->getHost() : 'ANY'),
'<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> '.$this->formatRouterConfig($requirements) ?: 'NO CUSTOM',
'<comment>Options</comment> '.$this->formatRouterConfig($route->getOptions()),
'<comment>Path-Regex</comment> '.$route->compile()->getRegex(),
);
if (isset($options['name'])) {
array_unshift($description, '<comment>Name</comment> '.$options['name']);
array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
}
if (null !== $route->compile()->getHostRegex()) {
$description[] = '<comment>Host-Regex</comment> '.$route->compile()->getHostRegex();
}
$this->writeText(implode("\n", $description)."\n", $options);
}
示例4: describeRoute
/**
* {@inheritdoc}
*/
protected function describeRoute(Route $route, array $options = array())
{
$requirements = $route->getRequirements();
unset($requirements['_scheme'], $requirements['_method']);
$output = '- Path: ' . $route->getPath() . "\n" . '- Host: ' . ('' !== $route->getHost() ? $route->getHost() : 'ANY') . "\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: ' . $this->formatRouterConfig($requirements) ?: 'NONE' . "\n" . '- Options: ' . $this->formatRouterConfig($route->getOptions()) . "\n" . '- Path-Regex: ' . $route->compile()->getRegex();
$this->write(isset($options['name']) ? $options['name'] . "\n" . str_repeat('-', strlen($options['name'])) . "\n\n" . $output : $output);
$this->write("\n");
}
示例5: testRequirements
public function testRequirements()
{
$route = new Route('/{foo}');
$route->setRequirements(array('foo' => '\\d+'));
$this->assertEquals(array('foo' => '\\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
$this->assertEquals('\\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
$this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
$route->setRequirements(array('foo' => '^\\d+$'));
$this->assertEquals('\\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the pattern');
$this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
}
示例6: access
/**
* {@inheritdoc}
*/
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account)
{
// Backup the original requirements.
$original_requirements = $route->getRequirements();
// Replace it with our entity access value and run the parent access check.
$route->setRequirement('_entity_access', $route->getRequirement('_page_access'));
$access = parent::access($route, $route_match, $account);
// Restore the original requirements.
$route->setRequirements($original_requirements);
return $access;
}
示例7: testRequirements
public function testRequirements()
{
$route = new Route('/{foo}');
$route->setRequirements(array('foo' => '\\d+'));
$this->assertEquals(array('foo' => '\\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
$this->assertEquals('\\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
$this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
$route->setRequirements(array('foo' => '^\\d+$'));
$this->assertEquals('\\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the pattern');
$this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
// test that an array requirement throws an exception
$this->setExpectedException('InvalidArgumentException');
$route->setRequirements(array('foo' => array('bar', 'baz')));
}
示例8: generate
/**
* {@inheritdoc}
*/
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
try {
if ($name instanceof SeoAwareInterface) {
$documentUrl = $name->getUrl();
} else {
throw new RouteNotFoundException();
}
$type = $this->collector->getDocumentType(get_class($name));
$route = new Route($documentUrl, ['_controller' => $this->routeMap[$type], 'document' => $name, 'type' => $type]);
// the Route has a cache of its own and is not recompiled as long as it does not get modified
$compiledRoute = $route->compile();
$hostTokens = $compiledRoute->getHostTokens();
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, 'ongr_route', $referenceType, $hostTokens);
} catch (\Exception $e) {
throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
}
}
示例9: applies
/**
* Implements AccessCheckInterface::applies().
*/
public function applies(Route $route)
{
$requirements = $route->getRequirements();
if (array_key_exists('_access_rest_csrf', $requirements)) {
if (isset($requirements['_method'])) {
// There could be more than one method requirement separated with '|'.
$methods = explode('|', $requirements['_method']);
// CSRF protection only applies to write operations, so we can filter
// out any routes that require reading methods only.
$write_methods = array_diff($methods, array('GET', 'HEAD', 'OPTIONS', 'TRACE'));
if (empty($write_methods)) {
return FALSE;
}
}
// No method requirement given, so we run this access check to be on the
// safe side.
return TRUE;
}
}
示例10: generate
/**
* {@inheritdoc}
*/
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
try {
$document = $parameters['document'];
if (is_object($document)) {
$documentUrl = $document->url;
} else {
$documentUrl = $document['url'];
}
$type = $this->collector->getDocumentType(get_class($document));
$route = new Route($documentUrl, ['_controller' => $this->routeMap[$type], 'document' => $document, 'type' => $type]);
// the Route has a cache of its own and is not recompiled as long as it does not get modified
$compiledRoute = $route->compile();
$hostTokens = $compiledRoute->getHostTokens();
$debug_message = $this->getRouteDebugMessage($name);
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens);
} catch (\Exception $e) {
throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
}
}
示例11: version
/**
* {@inheritdoc}
*/
public function version(Operation $operation, Result $result, Route $route)
{
if (!$operation->hasParameter($this->parameter)) {
return $operation;
}
$version = $operation->getParameter($this->parameter);
$operation->removeParameter($this->parameter);
if (!($versions = $version->getType()->getEnum())) {
$requirements = $route->getRequirements();
$versions = array($requirements[$this->parameter]);
}
$operations = array();
foreach ($versions as $version) {
$op = clone $operation;
$op->setVersion($version);
$op->setPath(str_replace(sprintf('{%s}', $this->parameter), $version, $op->getPath()));
$operations[] = $op;
}
return count($operations) === 1 ? current($operations) : $operations;
}
示例12: isAccessedAnonymously
private function isAccessedAnonymously($routeName, Route $route)
{
if (!in_array('GET', $route->getMethods()) && $route->getMethods()) {
return false;
// GET method must be allowed
}
if (strpos($routeName, '_') === 0) {
return false;
// internal|private routes
}
$compiled = $route->compile();
$params = [];
foreach ($compiled->getPathVariables() as $key) {
$params[$key] = 'any';
// we do not care about it
}
foreach ($route->getRequirements() as $key => $regex) {
$params[$key] = 'any';
// we do not care about it
}
foreach ($route->getDefaults() as $key => $default) {
$params[$key] = $default;
}
if (!array_key_exists('_controller', $params)) {
return false;
// route is dynamic, should not be index by robots
}
$uri = $this->get('router')->generate($routeName, $params);
// mock the request
$request = Request::create('http://mock.com' . $uri);
$request->setSession(new Session(new MockFileSessionStorage()));
// run the request through security firewall
$event = new GetResponseEvent($this->getApplication()->getKernel(), $request, HttpKernelInterface::MASTER_REQUEST);
try {
$this->get('security.firewall')->onKernelRequest($event);
} catch (AccessDeniedException $e) {
return false;
// access is denied
}
return !$event->getResponse() instanceof RedirectResponse;
}
示例13: applies
/**
* {@inheritdoc}
*/
public function applies(Route $route)
{
$requirements = $route->getRequirements();
// Check for current requirement _csrf_request_header_token and deprecated
// REST requirement.
$applicable_requirements = ['_csrf_request_header_token', '_access_rest_csrf'];
$requirement_keys = array_keys($requirements);
if (array_intersect($applicable_requirements, $requirement_keys)) {
if (isset($requirements['_method'])) {
// There could be more than one method requirement separated with '|'.
$methods = explode('|', $requirements['_method']);
// CSRF protection only applies to write operations, so we can filter
// out any routes that require reading methods only.
$write_methods = array_diff($methods, array('GET', 'HEAD', 'OPTIONS', 'TRACE'));
if (empty($write_methods)) {
return FALSE;
}
}
// No method requirement given, so we run this access check to be on the
// safe side.
return TRUE;
}
}
示例14: getInternalPathFromRoute
/**
* Gets the path of a route.
*
* @param \Symfony\Component\Routing\Route $route
* The route object.
* @param array $parameters
* An array of parameters as passed to
* \Symfony\Component\Routing\Generator\UrlGeneratorInterface::generate().
*
* @return string
* The url path corresponding to the route, without the base path.
*/
protected function getInternalPathFromRoute(SymfonyRoute $route, $parameters = array())
{
// The Route has a cache of its own and is not recompiled as long as it does
// not get modified.
$compiledRoute = $route->compile();
$hostTokens = $compiledRoute->getHostTokens();
$route_requirements = $route->getRequirements();
// We need to bypass the doGenerate() method's handling of absolute URLs as
// we handle that ourselves after processing the path.
if (isset($route_requirements['_scheme'])) {
unset($route_requirements['_scheme']);
}
$path = $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route_requirements, $compiledRoute->getTokens(), $parameters, $route->getPath(), FALSE, $hostTokens);
// The URL returned from doGenerate() will include the base path if there is
// one (i.e., if running in a subdirectory) so we need to strip that off
// before processing the path.
$base_url = $this->context->getBaseUrl();
if (!empty($base_url) && strpos($path, $base_url) === 0) {
$path = substr($path, strlen($base_url));
}
return $path;
}
示例15: setRequirements
/**
* Set the requirements
*
* @param array $requirements
* @return Route
*/
public function setRequirements(array $requirements)
{
parent::setRequirements($requirements);
$this->requirements = parent::getRequirements();
return $this;
}