Skip to content

Commit 960e777

Browse files
esi controller
1 parent 9ee6b34 commit 960e777

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: radek
5+
* Date: 13/09/2018
6+
* Time: 16:50
7+
*/
8+
9+
namespace Docplanner\AssetsBundle\Controller;
10+
11+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
class AssetsController extends Controller
16+
{
17+
public function scriptTagsAction(Request $request, $assetsType, $forRoute)
18+
{
19+
$scripts = [];
20+
21+
$assetsLoader = $this->get('docplanner_assets_bundle.service.assets_loader');
22+
23+
foreach($assetsLoader->assets($assetsType, false, $forRoute) as $script)
24+
{
25+
$scripts[] = sprintf('<script defer="defer" src="%s"></script>', $script);
26+
}
27+
28+
return new Response(implode("\n", $scripts));
29+
}
30+
31+
}

AssetsBundle/DependencyInjection/DocplannerAssetsExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function load(array $configs, ContainerBuilder $container)
6363
$manifest = @json_decode($manifest, true) ?? [];
6464
}
6565
else
66-
{
67-
throw new \RuntimeException(sprintf('Manifest file `%s` not found', $manifestFile));
68-
}
66+
{
67+
throw new \RuntimeException(sprintf('Manifest file `%s` not found', $manifestFile));
68+
}
6969
}
7070

7171
foreach($typeConfig['manifest_assets'] ?? [] as $manifestKey)

AssetsBundle/Service/AssetsLoader.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ public function __construct(AssetsProvider $assetsProvider)
1717

1818
/**
1919
* @param string $type
20-
* @param bool $inline
20+
* @param bool $inline
21+
* @param null $forceRoute
2122
*
2223
* @return string[]
2324
*/
24-
public function assets($type, $inline = false)
25+
public function assets($type, $inline = false, $forceRoute = null)
2526
{
2627
$result = [];
27-
foreach ($this->assetsProvider->getAssets($type) as $assetName => $asset)
28-
{
29-
if ($inline !== $asset->isInline())
30-
{
28+
foreach ($this->assetsProvider->getAssets($type, $forceRoute) as $assetName => $asset) {
29+
if ($inline !== $asset->isInline()) {
3130
continue;
3231
}
3332

AssetsBundle/Service/AssetsPicker.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,42 @@ class AssetsPicker
2222
private $config;
2323

2424
/**
25-
* @param RequestStack $requestStack
26-
* @param array $config
25+
* @param RequestStack $requestStack
26+
* @param array $config
2727
*/
2828
public function __construct(RequestStack $requestStack, array $config)
2929
{
3030
$this->requestStack = $requestStack;
31-
$this->config = $config;
31+
$this->config = $config;
3232
}
3333

3434
/**
3535
* @param string $type
3636
*
37+
* @param $forceRoute
3738
* @return string[] - asset names
3839
*/
39-
public function pickAssets($type)
40+
public function pickAssets($type, $forceRoute = null)
4041
{
41-
if (false === array_key_exists($type, $this->config['types']))
42-
{
42+
if (false === array_key_exists($type, $this->config['types'])) {
4343
throw new \LogicException(sprintf('Type "%s" is not defined!', $type));
4444
}
4545

46-
$route = $this->requestStack->getMasterRequest() ? $this->requestStack->getMasterRequest()->get('_route') : null;
46+
$route = $forceRoute ? $forceRoute : ($this->requestStack->getMasterRequest() ? $this->requestStack->getMasterRequest()->get('_route') : null);
4747

4848
$defaults = [];
4949
$picked = [];
50-
foreach ($this->config['types'][$type]['groups'] as $groupName => $group)
51-
{
52-
if (is_array($group['routes']) && in_array($route, $group['routes']))
53-
{
50+
foreach ($this->config['types'][$type]['groups'] as $groupName => $group) {
51+
if (is_array($group['routes']) && in_array($route, $group['routes'])) {
5452
$picked = array_merge($picked, $group['assets']);
5553
}
5654

57-
if ($group['default'])
58-
{
55+
if ($group['default']) {
5956
$defaults = array_merge($defaults, $group['assets']);
6057
}
6158
}
6259

63-
if (0 === count($picked))
64-
{
60+
if (0 === count($picked)) {
6561
array_unique($defaults);
6662

6763
return $defaults;

AssetsBundle/Service/AssetsProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public function __construct(AssetsRepository $assetsRepository, AssetsPicker $as
2828

2929
/**
3030
* @param string $type
31+
* @param $forceRoute
3132
*
3233
* @return Asset[]
3334
*/
34-
public function getAssets($type)
35+
public function getAssets($type, $forceRoute = null)
3536
{
36-
$chosenAssets = $this->assetsPicker->pickAssets($type);
37+
$chosenAssets = $this->assetsPicker->pickAssets($type, $forceRoute);
3738
if ([] === $chosenAssets)
3839
{
3940
return [];

0 commit comments

Comments
 (0)