Skip to content

Commit

Permalink
LoaderBundleRoutesTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Aug 17, 2021
1 parent 11fdc47 commit 3e26c28
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
43 changes: 43 additions & 0 deletions Services/Utils/LoaderBundleRoutesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Services\Utils;

use InvalidArgumentException;
use Prokl\BitrixSymfonyRouterBundle\Services\Router\InitRouter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;

/**
* Trait LoaderBundleRoutesTrait
* @package Prokl\BitrixSymfonyRouterBundle\Services\Utils
*
* @since 17.08.2021
*/
trait LoaderBundleRoutesTrait
{
/**
* Загрузить роуты в бандле.
*
* @param string $path Путь к конфигу.
* @param string $config Конфигурационный файл.
*
* @return void
*
* @throws InvalidArgumentException Нет класса-конфигуратора роутов.
*/
private function loadRoutes(string $path, string $config = 'routes.yaml') : void
{
$routeLoader = new YamlFileLoader(
new FileLocator($path)
);

$routes = $routeLoader->load($config);

if (class_exists(InitRouter::class)) {
InitRouter::addRoutesBundle($routes);
return;
}

throw new InvalidArgumentException('Class InitRouter not exist.');
}
}
43 changes: 42 additions & 1 deletion readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,45 @@ $agnosticRouterInstance = new Router(

```php
$router = \Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\SymfonyRoutes::getInstance();
```
```

2) Как загрузить роуты бандлы:

В файле `Extension` бандла:

```php
public function load(array $configs, ContainerBuilder $container) : void
{
// ....
$this->loadRoutes(__DIR__ . '/../Resources/config', 'routes.yaml');
}

/**
* Загрузить роуты в бандле.
*
* @param string $path Путь к конфигу.
* @param string $config Конфигурационный файл.
*
* @return void
*
* @throws InvalidArgumentException Нет класса-конфигуратора роутов.
*/
private function loadRoutes(string $path, string $config = 'routes.yaml') : void
{
$routeLoader = new \Symfony\Component\Routing\Loader\YamlFileLoader(
new FileLocator($path)
);

$routes = $routeLoader->load($config);

if (class_exists(InitRouter::class)) {
InitRouter::addRoutesBundle($routes);
return;
}

throw new InvalidArgumentException('Class InitRouter not exist.');
}
```

Или воспользоваться трэйтом `Prokl\BitrixSymfonyRouterBundle\Services\Utils\LoaderBundleRoutesTrait`,
куда вынесен этот метод.

0 comments on commit 3e26c28

Please sign in to comment.