ComPress
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
ComPress is a Wordpress plugin that helps to use composer with a lazy loader service container (dependency injector)., (*2)
Install
Usage
``` php
echo $container->get('twig.templating')->render('index.twig.html', ['hehh' => 'Yeah!']);, (*3)
## Documentation
ComPress plugin makes the marvellous Composer accessible in any WordPress project. If you never heard about it, Composer is a PHP package manager with a very good dependency resolver that makes using of third party libraries a breeze.
Composer transformed the PHP ecosystem recently so you should probably learn it. If you want to use the latest Twig templating engine in your WordPress plugins or themes you only have to
1. run composer require twig/twig in the moduleâs root dir
2. add the new package to the lazy loader service containerâs setup
Afterwards the Twig rendering engine will be accessible from anywhere this way:
```php
echo $container->get('twig.templating')->render('index.twig.html', ['hehh' => 'Yeah!']);
How to add a package to the container
Every package has a different way of initializing, the container helps collect them under a unified API -- and besides it helps decoupling your code (decoupling makes your code more maintanable and solid), (*4)
Twigâs documentation says this is the way you can instantiates Twig after installing it:, (*5)
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
'cache' => '/path/to/compilation_cache',
));
echo $twig->render('index.html', array('name' => 'Fabien'));
So the container needs to instantiate two classes, the Twig_Loader_Filesystem and using it as an argument the Twig_Environment class, that can be used to rendering directly. The container can solve this dependency easily if we define two services, and use the first as an argument of the second:, (*6)
//wp-content/plugins/evista-composer/Composer.php
/**
* Add service definitions here:
*
*/
public function setUpServices(){
$this->services = [
'twig.loader' =>[
'class' => '\Twig_Loader_Filesystem',
'arguments' => [__DIR__.'/views']
],
'twig.templating' => [
'class' => '\Twig_Environment',
'arguments' => ['@twig.loader']
],
];
}
The Composer::services variable holds all the needed informations. Every one of them requires a unique name, that will identify them throughout WordPress. These names will be the keys of the services array, and the most important properties of the services in an array are the values., (*7)
In the above example, âtwig.templatingâ is the name, this is how it can be requested from the container:, (*8)
$templating = $container->get('twig.templating');
$page = $templating->render('index.twig.html', ['hehh' => 'Yeah!']);
In the configuration array above there are two important key value pairs: class and arguments., (*9)
The first tells the container which class should it use to instantiate the service (with namespaces), and second tells what argument to use in what order. So far strings, flat (one level) arrays and other services are supported as argument., (*10)
public function setUpServices(){
$this->services = [
'sample.service' =>[
'class' => '\SampleService',
'arguments' => ['string', '@other.service', ['first'=>'1', "second" => '2']]
],
// ..
];
}
And this is it. After modifying Composer you are ready to use your new package installed via composer., (*11)
Change log
Please see CHANGELOG for more information what has changed recently., (*12)
Testing
bash
$ phpunit
, (*13)
Contributing
Please see CONTRIBUTING for details., (*14)
Security
If you discover any security related issues, please email sera.balint@e-vista.hu instead of using the issue tracker., (*15)
Credits
License
The MIT License (MIT). Please see License File for more information., (*16)