File tree Expand file tree Collapse file tree 2 files changed +85
-2
lines changed Expand file tree Collapse file tree 2 files changed +85
-2
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Prokl \WpSymfonyRouterBundle \Services \Utils ;
4+
5+ use InvalidArgumentException ;
6+ use Prokl \WpSymfonyRouterBundle \Services \Router \InitRouter ;
7+ use Symfony \Component \Config \FileLocator ;
8+ use Symfony \Component \Routing \Loader \YamlFileLoader ;
9+
10+ /**
11+ * Trait LoaderBundleRoutesTrait
12+ * @package Prokl\WpSymfonyRouterBundle\Services\Utils
13+ * @since 17.08.2021
14+ */
15+ trait LoaderBundleRoutesTrait
16+ {
17+ /**
18+ * Загрузить роуты в бандле.
19+ *
20+ * @param string $path Путь к конфигу.
21+ * @param string $config Конфигурационный файл.
22+ *
23+ * @return void
24+ *
25+ * @throws InvalidArgumentException Нет класса-конфигуратора роутов.
26+ */
27+ private function loadRoutes (string $ path , string $ config = 'routes.yaml ' ) : void
28+ {
29+ $ routeLoader = new YamlFileLoader (
30+ new FileLocator ($ path )
31+ );
32+
33+ $ routes = $ routeLoader ->load ($ config );
34+
35+ if (class_exists (InitRouter::class)) {
36+ InitRouter::addRoutesBundle ($ routes );
37+ return ;
38+ }
39+
40+ throw new InvalidArgumentException ('Class InitRouter not exist. ' );
41+ }
42+ }
Original file line number Diff line number Diff line change 88## Установка
99
10101 ) ` composer require proklung/wp.symfony.router.bundle `
11-
11+
12122 ) Подключение бандла в ` standalone_bundles.php `
1313
1414## Параметры
@@ -99,4 +99,45 @@ $agnosticRouterInstance = new Router(
9999
100100` ` ` php
101101$router = \P rokl\W pSymfonyRouterBundle\S ervices\A gnostic\R outesConfigurator::getInstance();
102- ` ` `
102+ ` ` `
103+
104+ 2) Как загрузить роуты бандлы :
105+
106+ В файле `Extension` бандла :
107+
108+ ` ` ` php
109+ public function load(array $configs, ContainerBuilder $container) : void
110+ {
111+ // ....
112+ $this->loadRoutes(__DIR__ . '/../Resources/config', 'routes.yaml');
113+ }
114+
115+ /**
116+ * Загрузить роуты в бандле.
117+ *
118+ * @param string $path Путь к конфигу.
119+ * @param string $config Конфигурационный файл.
120+ *
121+ * @return void
122+ *
123+ * @throws InvalidArgumentException Нет класса-конфигуратора роутов.
124+ */
125+ private function loadRoutes(string $path, string $config = 'routes.yaml') : void
126+ {
127+ $routeLoader = new \S ymfony\C omponent\R outing\L oader\Y amlFileLoader(
128+ new FileLocator($path)
129+ );
130+
131+ $routes = $routeLoader->load($config);
132+
133+ if (class_exists(InitRouter::class)) {
134+ InitRouter::addRoutesBundle($routes);
135+ return;
136+ }
137+
138+ throw new InvalidArgumentException('Class InitRouter not exist.');
139+ }
140+ ` ` `
141+
142+ Или воспользоваться трэйтом `Prokl\WpSymfonyRouterBundle\Services\Utils\LoaderBundleRoutesTrait`,
143+ куда вынесен этот метод.
You can’t perform that action at this time.
0 commit comments