Skip to content

Commit b998f3a

Browse files
committed
Allow to enable/disable standard concatenation and minification
1 parent ea06ba8 commit b998f3a

File tree

3 files changed

+203
-2
lines changed

3 files changed

+203
-2
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
# pug-assets
2-
Manage your assets and third-party transpiler (less, stylus, coffee, babel, etc.) and allow you to concat and/or minify them in production environment
1+
# Pug-Assets
2+
3+
Manage your assets and third-party transpiler (less, stylus, coffee, babel, etc.) and allow you to concat and/or minify them in production environment.
4+
5+
## Install
6+
In the root directory of your Symfony project, open a terminal and enter:
7+
```shell
8+
composer require pug-php/pug-assets
9+
```
10+
11+
Enable the plugin:
12+
```php
13+
use Pug\Pug;
14+
15+
$pug = new Pug();
16+
17+
// The facade syntax:
18+
Assets::enable($pug);
19+
$pug->render('... minify ...'); // here you can use minfiy, assets or concat keywords to wrap your assets
20+
21+
Assets::disable($pug);
22+
$pug->render('... minify ...'); // here minfiy, assets or concat are simple tags again
23+
24+
// Of the instanciation syntax:
25+
$assets = new Assets($pug);
26+
$pug->render('... minify ...'); // here you can use minfiy, assets or concat keywords to wrap your assets
27+
28+
unset($assets);
29+
$pug->render('... minify ...'); // here minfiy, assets or concat are simple tags again
30+
```
31+
For more information about the concat/minify usage, see https://github.com/pug-php/pug-minify#readme

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "pug-php/pug-assets",
3+
"description": "Pug template assets manager",
4+
"type": "library",
5+
"require": {
6+
"pug-php/pug": "^2.0.0",
7+
"pug-php/pug-minify": "^1.0",
8+
"pug-php/pug-filter-stylus": "^2.0",
9+
"pug-php/pug-filter-coffee-script": "^1.2",
10+
"pug-php/pug-filter-markdown": "^1.1"
11+
},
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Kyle Katarn",
16+
"email": "kylekatarnls@gmail.com"
17+
}
18+
],
19+
"minimum-stability": "stable",
20+
"autoload": {
21+
"psr-0": {
22+
"Pug": "src/"
23+
}
24+
}
25+
}

src/Pug/Assets.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Pug;
4+
5+
use Jade\Jade;
6+
use Pug\Keyword\Minify;
7+
8+
class Assets
9+
{
10+
/**
11+
* @var Pug|Jade $pug
12+
*/
13+
protected $pug;
14+
15+
/**
16+
* @var Minify $minify
17+
*/
18+
protected $minify;
19+
20+
/**
21+
* @var array $links
22+
*/
23+
protected static $links;
24+
25+
/**
26+
* Assets constructor.
27+
*
28+
* @param Pug $pug
29+
*/
30+
public function __construct(Jade $pug)
31+
{
32+
$this->pug = $pug;
33+
$this->setMinify(new Minify($pug));
34+
}
35+
36+
/**
37+
* @param Pug|Jade $pug
38+
*
39+
* @return Assets $assets
40+
*/
41+
public static function enable(Jade $pug)
42+
{
43+
$assets = new static($pug);
44+
static::$links[] = $assets;
45+
46+
return $assets;
47+
}
48+
49+
/**
50+
* @param Pug|Jade $pug
51+
*
52+
* @return Assets $assets
53+
*/
54+
public static function disable(Jade $pug)
55+
{
56+
$assets = null;
57+
static::$links = array_filter(static::$links, function ($entry) use (&$assets, $pug) {
58+
if ($entry->getPug() === $pug) {
59+
$assets = $entry->unsetMinify();
60+
61+
return false;
62+
}
63+
64+
return true;
65+
});
66+
67+
return $assets;
68+
}
69+
70+
/**
71+
* @param string $environment
72+
*
73+
* @return self $this
74+
*/
75+
public function setEnvironment($environment)
76+
{
77+
$this->pug->setCustomOption('environment', $environment);
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* @return string $environment
84+
*/
85+
public function getEnvironment()
86+
{
87+
return $this->pug->getOption('environment') ?: 'production';
88+
}
89+
90+
/**
91+
* @return Minify
92+
*/
93+
public function getMinify()
94+
{
95+
return $this->minify;
96+
}
97+
98+
/**
99+
* @return Jade|Pug
100+
*/
101+
public function getPug()
102+
{
103+
return $this->pug;
104+
}
105+
106+
/**
107+
* @param Minify $minify
108+
*
109+
* @return self $this
110+
*/
111+
public function setMinify($minify)
112+
{
113+
$this->pug->setKeyword('assets', $minify);
114+
$this->pug->setKeyword('concat', $minify);
115+
$this->pug->setKeyword('concat-to', $minify);
116+
$this->pug->setKeyword('minify', $minify);
117+
$this->pug->setKeyword('minify-to', $minify);
118+
$this->minify = $minify;
119+
120+
return $this;
121+
}
122+
123+
/**
124+
* Remove the keywords.
125+
*
126+
* @return self $this
127+
*/
128+
public function unsetMinify()
129+
{
130+
$this->pug->removeKeyword('assets');
131+
$this->pug->removeKeyword('concat');
132+
$this->pug->removeKeyword('concat-to');
133+
$this->pug->removeKeyword('minify');
134+
$this->pug->removeKeyword('minify-to');
135+
$this->minify = null;
136+
137+
return $this;
138+
}
139+
140+
/**
141+
* Assets destructor.
142+
*/
143+
public function __destruct()
144+
{
145+
$this->unsetMinify();
146+
}
147+
}

0 commit comments

Comments
 (0)