Skip to content

Commit bc065ba

Browse files
committed
LoaderBundleRoutesTrait
1 parent 21d3b25 commit bc065ba

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

readme.MD

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## Установка
99

1010
1) `composer require proklung/wp.symfony.router.bundle`
11-
11+
1212
2) Подключение бандла в `standalone_bundles.php`
1313

1414
## Параметры
@@ -99,4 +99,45 @@ $agnosticRouterInstance = new Router(
9999

100100
```php
101101
$router = \Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator::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 \Symfony\Component\Routing\Loader\YamlFileLoader(
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+
куда вынесен этот метод.

0 commit comments

Comments
 (0)