ImportConfigurator.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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;
  11. use Symfony\Component\Routing\RouteCollection;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. */
  15. class ImportConfigurator
  16. {
  17. use Traits\HostTrait;
  18. use Traits\PrefixTrait;
  19. use Traits\RouteTrait;
  20. public function __construct(
  21. private RouteCollection $parent,
  22. RouteCollection $route,
  23. ) {
  24. $this->route = $route;
  25. }
  26. public function __serialize(): array
  27. {
  28. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  29. }
  30. public function __unserialize(array $data): void
  31. {
  32. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  33. }
  34. public function __destruct()
  35. {
  36. $this->parent->addCollection($this->route);
  37. }
  38. /**
  39. * Sets the prefix to add to the path of all child routes.
  40. *
  41. * @param string|array $prefix the prefix, or the localized prefixes
  42. *
  43. * @return $this
  44. */
  45. final public function prefix(string|array $prefix, bool $trailingSlashOnRoot = true): static
  46. {
  47. $this->addPrefix($this->route, $prefix, $trailingSlashOnRoot);
  48. return $this;
  49. }
  50. /**
  51. * Sets the prefix to add to the name of all child routes.
  52. *
  53. * @return $this
  54. */
  55. final public function namePrefix(string $namePrefix): static
  56. {
  57. $this->route->addNamePrefix($namePrefix);
  58. return $this;
  59. }
  60. /**
  61. * Sets the host to use for all child routes.
  62. *
  63. * @param string|array $host the host, or the localized hosts
  64. *
  65. * @return $this
  66. */
  67. final public function host(string|array $host): static
  68. {
  69. $this->addHost($this->route, $host);
  70. return $this;
  71. }
  72. }