RouteTrait.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Loader\Configurator\Traits;
  11. use Symfony\Component\Routing\Route;
  12. use Symfony\Component\Routing\RouteCollection;
  13. trait RouteTrait
  14. {
  15. /**
  16. * @var RouteCollection|Route
  17. */
  18. private $route;
  19. /**
  20. * Adds defaults.
  21. *
  22. * @return $this
  23. */
  24. final public function defaults(array $defaults)
  25. {
  26. $this->route->addDefaults($defaults);
  27. return $this;
  28. }
  29. /**
  30. * Adds requirements.
  31. *
  32. * @return $this
  33. */
  34. final public function requirements(array $requirements)
  35. {
  36. $this->route->addRequirements($requirements);
  37. return $this;
  38. }
  39. /**
  40. * Adds options.
  41. *
  42. * @return $this
  43. */
  44. final public function options(array $options)
  45. {
  46. $this->route->addOptions($options);
  47. return $this;
  48. }
  49. /**
  50. * Sets the condition.
  51. *
  52. * @return $this
  53. */
  54. final public function condition(string $condition)
  55. {
  56. $this->route->setCondition($condition);
  57. return $this;
  58. }
  59. /**
  60. * Sets the pattern for the host.
  61. *
  62. * @return $this
  63. */
  64. final public function host(string $pattern)
  65. {
  66. $this->route->setHost($pattern);
  67. return $this;
  68. }
  69. /**
  70. * Sets the schemes (e.g. 'https') this route is restricted to.
  71. * So an empty array means that any scheme is allowed.
  72. *
  73. * @param string[] $schemes
  74. *
  75. * @return $this
  76. */
  77. final public function schemes(array $schemes)
  78. {
  79. $this->route->setSchemes($schemes);
  80. return $this;
  81. }
  82. /**
  83. * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
  84. * So an empty array means that any method is allowed.
  85. *
  86. * @param string[] $methods
  87. *
  88. * @return $this
  89. */
  90. final public function methods(array $methods)
  91. {
  92. $this->route->setMethods($methods);
  93. return $this;
  94. }
  95. /**
  96. * Adds the "_controller" entry to defaults.
  97. *
  98. * @param callable|string $controller a callable or parseable pseudo-callable
  99. *
  100. * @return $this
  101. */
  102. final public function controller($controller)
  103. {
  104. $this->route->addDefaults(['_controller' => $controller]);
  105. return $this;
  106. }
  107. }