]> Raphaƫl G. Git Repositories - packbundle/blob - README.md
Add SluggerUtil class alias
[packbundle] / README.md
1 Installation
2 ============
3
4 Applications that use Symfony Flex
5 ----------------------------------
6
7 Add bundle custom repository to your project's `composer.json` file:
8
9 ```json
10 {
11 ...,
12 "repositories": [
13 {
14 "type": "package",
15 "package": {
16 "name": "rapsys/packbundle",
17 "version": "dev-master",
18 "source": {
19 "type": "git",
20 "url": "https://git.rapsys.eu/packbundle",
21 "reference": "master"
22 },
23 "autoload": {
24 "psr-4": {
25 "Rapsys\\PackBundle\\": ""
26 }
27 },
28 "require": {
29 "symfony/asset": "^4.4",
30 "symfony/flex": "^1.5",
31 "symfony/framework-bundle": "^4.4",
32 "symfony/process": "^4.4",
33 "symfony/twig-bundle": "^4.4",
34 "twig/extensions": "^1.5"
35 }
36 }
37 }
38 ],
39 ...
40 }
41 ```
42
43 Then open a command console, enter your project directory and execute:
44
45 ```console
46 $ composer require rapsys/packbundle dev-master
47 ```
48
49 Applications that don't use Symfony Flex
50 ----------------------------------------
51
52 ### Step 1: Download the Bundle
53
54 Open a command console, enter your project directory and execute the
55 following command to download the latest stable version of this bundle:
56
57 ```console
58 $ composer require rapsys/packbundle dev-master
59 ```
60
61 This command requires you to have Composer installed globally, as explained
62 in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
63 of the Composer documentation.
64
65 ### Step 2: Enable the Bundle
66
67 Then, enable the bundle by adding it to the list of registered bundles
68 in the `app/AppKernel.php` file of your project:
69
70 ```php
71 <?php
72 // app/AppKernel.php
73
74 // ...
75 class AppKernel extends Kernel
76 {
77 public function registerBundles()
78 {
79 $bundles = array(
80 // ...
81 new Rapsys\PackBundle\RapsysPackBundle(),
82 );
83
84 // ...
85 }
86
87 // ...
88 }
89 ```
90
91 ### Step 3: Configure the Bundle
92
93 Setup configuration file `config/packages/rapsys_pack.yaml` with the following
94 content available in `Resources/config/packages/rapsys_pack.yaml`:
95
96 ```yaml
97 #Services configuration
98 services:
99 #Replace assets.packages definition
100 assets.packages:
101 class: 'Symfony\Component\Asset\Packages'
102 arguments: [ '@rapsys_pack.path_package' ]
103 #Replace assets.context definition
104 assets.context:
105 class: 'Rapsys\PackBundle\Context\RequestStackContext'
106 arguments: [ '@request_stack', '%asset.request_context.base_path%', '%asset.request_context.secure%' ]
107 #Register assets pack package
108 rapsys_pack.path_package:
109 class: 'Rapsys\PackBundle\Package\PathPackage'
110 arguments: [ '/', '@assets.empty_version_strategy', '@assets.context' ]
111 public: true
112 #Register twig pack extension
113 rapsys_pack.pack_extension:
114 class: 'Rapsys\PackBundle\Extension\PackExtension'
115 arguments: [ '@file_locator', '@service_container', '@rapsys_pack.path_package', '@rapsys_pack.slugger_util' ]
116 tags: [ 'twig.extension' ]
117 #Register slugger utils service
118 rapsys_pack.slugger_util:
119 class: 'Rapsys\PackBundle\Util\SluggerUtil'
120 arguments: [ '%env(APP_SECRET)%' ]
121 public: true
122 ```
123
124 Open a command console, enter your project directory and execute the following
125 command to see default bundle configuration:
126
127 ```console
128 $ php bin/console config:dump-reference RapsysPackBundle
129 ```
130
131 Open a command console, enter your project directory and execute the following
132 command to see current bundle configuration:
133
134 ```console
135 $ php bin/console debug:config RapsysPackBundle
136 ```
137
138 ### Step 4: Use the twig extension in your Template
139
140 You can use a template like this to generate your first `rapsys_pack` enabled
141 template:
142
143 ```twig
144 <!DOCTYPE html>
145 <html>
146 <head>
147 <meta charset="UTF-8" />
148 <title>{% block title %}Welcome!{% endblock %}</title>
149 {% stylesheet '//fonts.googleapis.com/css?family=Irish+Grover|La+Belle+Aurore' '@NamedBundle/Resources/public/css/{reset,screen}.css' '@Short/css/example.css' %}
150 <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
151 {% endstylesheet %}
152 </head>
153 <body>
154 {% block body %}{% endblock %}
155 {% javascript '@Short/js/*.js' %}
156 <script type="text/javascript" src="{{ asset_url }}"></script>
157 {% endjavascript %}
158 </body>
159 </html>
160 ```
161
162 ### Step 5: Make sure you have local binary installed
163
164 You need to have cpack and jpack scripts from https://git.rapsys.eu/packer/ repository
165 set as executable and installed in /usr/local/bin.
166
167 To install cpack and jpack required packages open a root console and execute the
168 following command:
169
170 ```console
171 # urpmi perl-base perl-CSS-Packer perl-JavaScript-Packer
172 ```
173
174 or stone age distributions:
175
176 ```console
177 # apt-get install libcss-packer-perl libjavascript-packer-perl
178 ```
179
180 or other distributions through cpan:
181
182 ```console
183 # cpan App::cpanminus
184 # cpanm CSS::Packer
185 # cpanm JavaScript::Packer
186 ```
187
188 ### Step 6: Create your own filter
189
190 You can create you own mypackfilter class which call a mypack binary:
191
192 ```php
193 <?php
194
195 namespace Rapsys\PackBundle\Filter;
196
197 use Twig\Error\Error;
198
199 //This class will be defined in the parameter rapsys_pack.filters.(css|img|js).[x].class string
200 class MyPackFilter implements FilterInterface {
201 //The constructor arguments ... will be replaced defined in the parameter rapsys_pack.filters.(css|img|js).[x].args array
202 public function __construct(string $fileName, int $line, string $bin = 'mypack', ...) {
203 //Set fileName
204 $this->fileName = $fileName;
205
206 //Set line
207 $this->line = $line;
208
209 //Set bin
210 $this->bin = $bin;
211
212 //Check argument presence
213 if (!empty($this->...)) {
214 //Append argument
215 if ($this->... == '...') {
216 $this->bin .= ' ...';
217 } else {
218 //Throw an error on ...
219 throw new Error(sprintf('Got ... for %s: %s', $this->bin, $this->...), $this->line, $this->fileName);
220 }
221 }
222 }
223
224 //Pass merge of all inputs in content
225 public function process(string $content): string {
226 //Create descriptors
227 $descriptorSpec = array(
228 0 => array('pipe', 'r'),
229 1 => array('pipe', 'w'),
230 2 => array('pipe', 'w')
231 );
232
233 //Open process
234 if (is_resource($proc = proc_open($this->bin, $descriptorSpec, $pipes))) {
235 //Set stderr as non blocking
236 stream_set_blocking($pipes[2], false);
237
238 //Send content to stdin
239 fwrite($pipes[0], $content);
240
241 //Close stdin
242 fclose($pipes[0]);
243
244 //Read content from stdout
245 if ($stdout = stream_get_contents($pipes[1])) {
246 $content = $stdout;
247 }
248
249 //Close stdout
250 fclose($pipes[1]);
251
252 //Read content from stderr
253 if (($stderr = stream_get_contents($pipes[2]))) {
254 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin, $stderr), $this->line, $this->fileName);
255 }
256
257 //Close stderr
258 fclose($pipes[2]);
259
260 //Close process
261 if (($ret = proc_close($proc))) {
262 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin, $ret), $this->line, $this->fileName);
263 }
264 }
265
266 //Return content
267 return $content;
268 }
269 }
270 ```
271
272 The class must implements FilterInterface and get it's arguments through constructor.