RouteTrait.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. protected RouteCollection|Route $route;
  16. /**
  17. * Adds defaults.
  18. *
  19. * @return $this
  20. */
  21. final public function defaults(array $defaults): static
  22. {
  23. $this->route->addDefaults($defaults);
  24. return $this;
  25. }
  26. /**
  27. * Adds requirements.
  28. *
  29. * @return $this
  30. */
  31. final public function requirements(array $requirements): static
  32. {
  33. $this->route->addRequirements($requirements);
  34. return $this;
  35. }
  36. /**
  37. * Adds options.
  38. *
  39. * @return $this
  40. */
  41. final public function options(array $options): static
  42. {
  43. $this->route->addOptions($options);
  44. return $this;
  45. }
  46. /**
  47. * Whether paths should accept utf8 encoding.
  48. *
  49. * @return $this
  50. */
  51. final public function utf8(bool $utf8 = true): static
  52. {
  53. $this->route->addOptions(['utf8' => $utf8]);
  54. return $this;
  55. }
  56. /**
  57. * Sets the condition.
  58. *
  59. * @return $this
  60. */
  61. final public function condition(string $condition): static
  62. {
  63. $this->route->setCondition($condition);
  64. return $this;
  65. }
  66. /**
  67. * Sets the pattern for the host.
  68. *
  69. * @return $this
  70. */
  71. final public function host(string $pattern): static
  72. {
  73. $this->route->setHost($pattern);
  74. return $this;
  75. }
  76. /**
  77. * Sets the schemes (e.g. 'https') this route is restricted to.
  78. * So an empty array means that any scheme is allowed.
  79. *
  80. * @param string[] $schemes
  81. *
  82. * @return $this
  83. */
  84. final public function schemes(array $schemes): static
  85. {
  86. $this->route->setSchemes($schemes);
  87. return $this;
  88. }
  89. /**
  90. * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
  91. * So an empty array means that any method is allowed.
  92. *
  93. * @param string[] $methods
  94. *
  95. * @return $this
  96. */
  97. final public function methods(array $methods): static
  98. {
  99. $this->route->setMethods($methods);
  100. return $this;
  101. }
  102. /**
  103. * Adds the "_controller" entry to defaults.
  104. *
  105. * @param callable|string|array $controller a callable or parseable pseudo-callable
  106. *
  107. * @return $this
  108. */
  109. final public function controller(callable|string|array $controller): static
  110. {
  111. $this->route->addDefaults(['_controller' => $controller]);
  112. return $this;
  113. }
  114. /**
  115. * Adds the "_locale" entry to defaults.
  116. *
  117. * @return $this
  118. */
  119. final public function locale(string $locale): static
  120. {
  121. $this->route->addDefaults(['_locale' => $locale]);
  122. return $this;
  123. }
  124. /**
  125. * Adds the "_format" entry to defaults.
  126. *
  127. * @return $this
  128. */
  129. final public function format(string $format): static
  130. {
  131. $this->route->addDefaults(['_format' => $format]);
  132. return $this;
  133. }
  134. /**
  135. * Adds the "_stateless" entry to defaults.
  136. *
  137. * @return $this
  138. */
  139. final public function stateless(bool $stateless = true): static
  140. {
  141. $this->route->addDefaults(['_stateless' => $stateless]);
  142. return $this;
  143. }
  144. }