4 Applications that use Symfony Flex
5 ----------------------------------
7 Add bundle custom repository to your project's `composer.json` file:
16 "name": "rapsys/packbundle",
17 "version": "dev-master",
20 "url": "https://git.rapsys.eu/packbundle",
25 "Rapsys\\PackBundle\\": ""
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"
43 Then open a command console, enter your project directory and execute:
46 $ composer require rapsys/packbundle dev-master
49 Applications that don't use Symfony Flex
50 ----------------------------------------
52 ### Step 1: Download the Bundle
54 Open a command console, enter your project directory and execute the
55 following command to download the latest stable version of this bundle:
58 $ composer require rapsys/packbundle dev-master
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.
65 ### Step 2: Enable the Bundle
67 Then, enable the bundle by adding it to the list of registered bundles
68 in the `app/AppKernel.php` file of your project:
75 class AppKernel extends Kernel
77 public function registerBundles()
81 new Rapsys\PackBundle\RapsysPackBundle(),
91 ### Step 3: Configure the Bundle
93 Setup configuration file `config/packages/rapsys_pack.yaml` with the following
94 content available in `Resources/config/packages/rapsys_pack.yaml`:
97 #Services configuration
99 #Replace assets.packages definition
101 class: 'Symfony\Component\Asset\Packages'
102 arguments: [ '@rapsys_pack.path_package' ]
103 #Replace assets.context definition
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' ]
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 util service
118 rapsys_pack.slugger_util:
119 class: 'Rapsys\PackBundle\Util\SluggerUtil'
120 arguments: [ '%kernel.secret%' ]
122 #Register slugger util class alias
123 Rapsys\PackBundle\Util\SluggerUtil:
124 alias: 'rapsys_pack.slugger_util'
127 Open a command console, enter your project directory and execute the following
128 command to see default bundle configuration:
131 $ php bin/console config:dump-reference RapsysPackBundle
134 Open a command console, enter your project directory and execute the following
135 command to see current bundle configuration:
138 $ php bin/console debug:config RapsysPackBundle
141 ### Step 4: Use the twig extension in your Template
143 You can use a template like this to generate your first `rapsys_pack` enabled
150 <meta charset="UTF-8" />
151 <title>{% block title %}Welcome!{% endblock %}</title>
152 {% stylesheet '//fonts.googleapis.com/css?family=Irish+Grover|La+Belle+Aurore' '@NamedBundle/Resources/public/css/{reset,screen}.css' '@Short/css/example.css' %}
153 <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
157 {% block body %}{% endblock %}
158 {% javascript '@Short/js/*.js' %}
159 <script type="text/javascript" src="{{ asset_url }}"></script>
165 ### Step 5: Make sure you have local binary installed
167 You need to have cpack and jpack scripts from https://git.rapsys.eu/packer/ repository
168 set as executable and installed in /usr/local/bin.
170 To install cpack and jpack required packages open a root console and execute the
174 # urpmi perl-base perl-CSS-Packer perl-JavaScript-Packer
177 or stone age distributions:
180 # apt-get install libcss-packer-perl libjavascript-packer-perl
183 or other distributions through cpan:
186 # cpan App::cpanminus
188 # cpanm JavaScript::Packer
191 ### Step 6: Create your own filter
193 You can create you own mypackfilter class which call a mypack binary:
198 namespace Rapsys\PackBundle\Filter;
200 use Twig\Error\Error;
202 //This class will be defined in the parameter rapsys_pack.filters.(css|img|js).[x].class string
203 class MyPackFilter implements FilterInterface {
204 //The constructor arguments ... will be replaced defined in the parameter rapsys_pack.filters.(css|img|js).[x].args array
205 public function __construct(string $fileName, int $line, string $bin = 'mypack', ...) {
207 $this->fileName = $fileName;
215 //Check argument presence
216 if (!empty($this->...)) {
218 if ($this->... == '...') {
219 $this->bin .= ' ...';
221 //Throw an error on ...
222 throw new Error(sprintf('Got ... for %s: %s', $this->bin, $this->...), $this->line, $this->fileName);
227 //Pass merge of all inputs in content
228 public function process(string $content): string {
230 $descriptorSpec = array(
231 0 => array('pipe', 'r'),
232 1 => array('pipe', 'w'),
233 2 => array('pipe', 'w')
237 if (is_resource($proc = proc_open($this->bin, $descriptorSpec, $pipes))) {
238 //Set stderr as non blocking
239 stream_set_blocking($pipes[2], false);
241 //Send content to stdin
242 fwrite($pipes[0], $content);
247 //Read content from stdout
248 if ($stdout = stream_get_contents($pipes[1])) {
255 //Read content from stderr
256 if (($stderr = stream_get_contents($pipes[2]))) {
257 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin, $stderr), $this->line, $this->fileName);
264 if (($ret = proc_close($proc))) {
265 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin, $ret), $this->line, $this->fileName);
275 The class must implements FilterInterface and get it's arguments through constructor.