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 utils service
118 rapsys_pack.slugger_util:
119 class: 'Rapsys\PackBundle\Util\SluggerUtil'
120 arguments: [ '%env(APP_SECRET)%' ]
124 Open a command console, enter your project directory and execute the following
125 command to see default bundle configuration:
128 $ php bin/console config:dump-reference RapsysPackBundle
131 Open a command console, enter your project directory and execute the following
132 command to see current bundle configuration:
135 $ php bin/console debug:config RapsysPackBundle
138 ### Step 4: Use the twig extension in your Template
140 You can use a template like this to generate your first `rapsys_pack` enabled
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 }}" />
154 {% block body %}{% endblock %}
155 {% javascript '@Short/js/*.js' %}
156 <script type="text/javascript" src="{{ asset_url }}"></script>
162 ### Step 5: Make sure you have local binary installed
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.
167 To install cpack and jpack required packages open a root console and execute the
171 # urpmi perl-base perl-CSS-Packer perl-JavaScript-Packer
174 or stone age distributions:
177 # apt-get install libcss-packer-perl libjavascript-packer-perl
180 or other distributions through cpan:
183 # cpan App::cpanminus
185 # cpanm JavaScript::Packer
188 ### Step 6: Create your own filter
190 You can create you own mypackfilter class which call a mypack binary:
195 namespace Rapsys\PackBundle\Filter;
197 use Twig\Error\Error;
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', ...) {
204 $this->fileName = $fileName;
212 //Check argument presence
213 if (!empty($this->...)) {
215 if ($this->... == '...') {
216 $this->bin .= ' ...';
218 //Throw an error on ...
219 throw new Error(sprintf('Got ... for %s: %s', $this->bin, $this->...), $this->line, $this->fileName);
224 //Pass merge of all inputs in content
225 public function process(string $content): string {
227 $descriptorSpec = array(
228 0 => array('pipe', 'r'),
229 1 => array('pipe', 'w'),
230 2 => array('pipe', 'w')
234 if (is_resource($proc = proc_open($this->bin, $descriptorSpec, $pipes))) {
235 //Set stderr as non blocking
236 stream_set_blocking($pipes[2], false);
238 //Send content to stdin
239 fwrite($pipes[0], $content);
244 //Read content from stdout
245 if ($stdout = stream_get_contents($pipes[1])) {
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);
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);
272 The class must implements FilterInterface and get it's arguments through constructor.