Skip to content

Commit e0a803b

Browse files
committed
Use assetc picker service
1 parent c4f982b commit e0a803b

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

AssetsBundle/Resources/config/services.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ parameters:
55

66
services:
77
docplanner_assets_bundle.twig.assets:
8-
class: %docplanner_assets_bundle.twig.assets.class%
8+
class: %docplanner_assets_bundle.twig.assets.class%
99
arguments: [ @docplanner_assets_bundle.service.assets_loader ]
1010
tags:
1111
- { name: twig.extension }
1212

1313
docplanner_assets_bundle.service.assets_loader:
14-
class: %docplanner_assets_bundle.service.assets_loader.class%
14+
class: %docplanner_assets_bundle.service.assets_loader.class%
15+
arguments: [ @docplanner_assets.service.assets_picker ]
1516

1617
docplanner_assets.service.assets_picker:
1718
class: %docplanner_assets.service.assets_picker.class%

AssetsBundle/Service/AssetsLoader.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,93 @@
22

33
namespace Docplanner\AssetsBundle\Service;
44

5+
use Docplanner\AssetsBundle\IO\Asset;
6+
57
class AssetsLoader
68
{
9+
10+
/** @var AssetsPicker $assetsPicker */
11+
private $asstsPicker;
12+
13+
/**
14+
* @param AssetsPicker $assetsPicker
15+
*/
16+
public function __construct(AssetsPicker $assetsPicker)
17+
{
18+
$this->asstsPicker = $assetsPicker;
19+
}
20+
721
/**
822
* @param null $type
923
*
1024
* @return string
1125
*/
12-
public function renderScript($type = 'file')
26+
public function renderScript($type = 'src')
1327
{
14-
$files = ['platform/js/rwd-common.js'];
28+
$assets = $this->asstsPicker->pickScriptAssets();
29+
$mask = $type == 'inline' ? '<script>%s</script>' : '<script src="%s?%s"></script>';
1530

16-
$mask = $type == 'inline' ? '<script>%s</script>' : '<script src="%s?%s"></script>';
17-
18-
return $this->render($mask, $files, $type);
31+
return $this->render($mask, $assets, $type);
1932
}
2033

2134
/**
2235
* @param null $type
2336
*
2437
* @return string
2538
*/
26-
public function renderStyle($type = 'file')
39+
public function renderStyle($type = 'src')
2740
{
28-
$files = ['platform/css/rwd-common.css'];
29-
30-
$mask = $type == 'inline' ? '<style>%s</style>' : '<link rel="stylesheet" type="text/css" href="%s?%s">';
41+
$assets = $this->asstsPicker->pickStyleAssets();
42+
$mask = $type == 'inline' ? '<style>%s</style>' : '<link rel="stylesheet" type="text/css" href="%s?%s">';
3143

32-
return $this->render($mask, $files, $type);
44+
return $this->render($mask, $assets, $type);
3345
}
3446

3547
/**
36-
* @param $mask
37-
* @param $files
38-
* @param $type
48+
* @param $mask
49+
* @param Asset[] $assets
50+
* @param $type
3951
*
4052
* @return string
4153
*/
42-
private function render($mask, $files, $type)
54+
private function render($mask, $assets, $type)
4355
{
4456
$ret = '';
4557

46-
if ($type == 'file')
58+
if ($type == 'src')
4759
{
48-
foreach ($files as $file)
60+
foreach ($assets as $asset)
4961
{
50-
if (!file_exists($file))
62+
if (!file_exists($asset->getSrc()))
5163
{
52-
throw new \LogicException(sprintf('File %s not found', $file));
64+
throw new \LogicException(sprintf('File %s not found', $asset->getSrc()));
5365
}
5466

55-
$ret .= sprintf($mask, $file, crc32(file_get_contents($file)));
67+
if ($asset->getSrc())
68+
{
69+
$ret .= sprintf($mask, $asset->getSrc(), crc32(file_get_contents($asset->getSrc())));
70+
}
5671
}
5772
}
5873
else
5974
{
60-
foreach ($files as $file)
75+
foreach ($assets as $asset)
6176
{
62-
if (!file_exists($file))
77+
if (!file_exists($asset->getInline()))
6378
{
64-
throw new \LogicException(sprintf('File %s not found', $file));
79+
throw new \LogicException(sprintf('File %s not found', $asset->getInline()));
6580
}
6681

67-
$source = file_get_contents($file);
68-
$ret .= $source;
82+
if ($asset->getInline())
83+
{
84+
$ret .= file_get_contents($asset->getInline());
85+
}
6986
}
7087

71-
$ret = sprintf($mask, $ret);
88+
if ($ret)
89+
{
90+
$ret = sprintf($mask, $ret);
91+
}
7292
}
7393

7494
return $ret;

AssetsBundle/Twig/AssetsExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getFunctions()
3333
*
3434
* @return string
3535
*/
36-
public function assetsScript($type = 'file')
36+
public function assetsScript($type = 'src')
3737
{
3838
return $this->assetsLoader->renderScript($type);
3939
}
@@ -43,7 +43,7 @@ public function assetsScript($type = 'file')
4343
*
4444
* @return string
4545
*/
46-
public function assetsStyle($type = 'file')
46+
public function assetsStyle($type = 'src')
4747
{
4848
return $this->assetsLoader->renderStyle($type);
4949
}

0 commit comments

Comments
 (0)