Skip to content

Commit ab8cbe4

Browse files
committed
Refactoring and example config
1 parent e0a803b commit ab8cbe4

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Example
2+
#
3+
#docplanner_assets:
4+
# style:
5+
# assets:
6+
# common: { src: "/to/some/file.css" }
7+
# fries: { src: "/to/some/other/file.css", inline: true }
8+
# groups:
9+
# default:
10+
# assets:
11+
# - common
12+
# - fries
13+
# default: true
14+
# homepage:
15+
# assets:
16+
# - common
17+
# - fries
18+
# - homepage
19+
# routes: [ "homepage", "homepage_with_fireworks" ]
20+
# script:
21+
# assets:
22+
# common: { src: "/to/some/file.js" }
23+
# fries: { src: "/to/some/other/file.js", inline: true }
24+
# groups:
25+
# default:
26+
# assets:
27+
# - common
28+
# - fries
29+
# default: true
30+
# homepage:
31+
# assets:
32+
# - common
33+
# - fries
34+
# - homepage
35+
# routes: [ "homepage", "homepage_with_fireworks" ]

AssetsBundle/Service/AssetsLoader.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,46 @@ public function __construct(AssetsPicker $assetsPicker)
1919
}
2020

2121
/**
22-
* @param null $type
22+
* @param bool|false $isInline
2323
*
2424
* @return string
2525
*/
26-
public function renderScript($type = 'src')
26+
public function renderScript($isInline = false)
2727
{
2828
$assets = $this->asstsPicker->pickScriptAssets();
29-
$mask = $type == 'inline' ? '<script>%s</script>' : '<script src="%s?%s"></script>';
29+
$mask = $type == 'inline' ? '<script>%s</script>' : '<script src="%s?%s"></script>';
3030

31-
return $this->render($mask, $assets, $type);
31+
return $this->render($mask, $assets, $isInline);
3232
}
3333

3434
/**
35-
* @param null $type
35+
* @param bool|false $isInline
3636
*
3737
* @return string
3838
*/
39-
public function renderStyle($type = 'src')
39+
public function renderStyle($isInline = false)
4040
{
4141
$assets = $this->asstsPicker->pickStyleAssets();
42-
$mask = $type == 'inline' ? '<style>%s</style>' : '<link rel="stylesheet" type="text/css" href="%s?%s">';
42+
$mask = $type == 'inline' ? '<style>%s</style>' : '<link rel="stylesheet" type="text/css" href="%s?%s">';
4343

44-
return $this->render($mask, $assets, $type);
44+
return $this->render($mask, $assets, $isInline);
4545
}
4646

4747
/**
48-
* @param $mask
48+
* @param String $mask
4949
* @param Asset[] $assets
50-
* @param $type
50+
* @param boolean $isInline
5151
*
5252
* @return string
5353
*/
54-
private function render($mask, $assets, $type)
54+
private function render($mask, $assets, $isInline)
5555
{
5656
$ret = '';
5757

58-
if ($type == 'src')
58+
if (!$isInline)
5959
{
6060
foreach ($assets as $asset)
6161
{
62-
if (!file_exists($asset->getSrc()))
63-
{
64-
throw new \LogicException(sprintf('File %s not found', $asset->getSrc()));
65-
}
66-
6762
if ($asset->getSrc())
6863
{
6964
$ret .= sprintf($mask, $asset->getSrc(), crc32(file_get_contents($asset->getSrc())));
@@ -74,11 +69,6 @@ private function render($mask, $assets, $type)
7469
{
7570
foreach ($assets as $asset)
7671
{
77-
if (!file_exists($asset->getInline()))
78-
{
79-
throw new \LogicException(sprintf('File %s not found', $asset->getInline()));
80-
}
81-
8272
if ($asset->getInline())
8373
{
8474
$ret .= file_get_contents($asset->getInline());
@@ -94,6 +84,3 @@ private function render($mask, $assets, $type)
9484
return $ret;
9585
}
9686
}
97-
98-
99-

AssetsBundle/Twig/AssetsExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ public function getFunctions()
3333
*
3434
* @return string
3535
*/
36-
public function assetsScript($type = 'src')
36+
public function assetsScript($type = null)
3737
{
38-
return $this->assetsLoader->renderScript($type);
38+
return $this->assetsLoader->renderScript($type == 'inline' ? true : false);
3939
}
4040

4141
/**
4242
* @param null $type
4343
*
4444
* @return string
4545
*/
46-
public function assetsStyle($type = 'src')
46+
public function assetsStyle($type = null)
4747
{
48-
return $this->assetsLoader->renderStyle($type);
48+
return $this->assetsLoader->renderStyle($type == 'inline' ? true : false);
4949
}
5050

5151
/**

0 commit comments

Comments
 (0)