Skip to content
Snippets Groups Projects
Commit c2bd6e1b authored by Kevin Hill's avatar Kevin Hill
Browse files

Adding the back the accidental removal of Symfony support

parent 1ae1c12c
No related branches found
No related tags found
No related merge requests found
<?php
namespace Khill\Lavacharts\Symfony\Bundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class LavachartsBundle extends Bundle
{
//
}
parameters:
khill.lavacharts.class: Khill\Lavacharts\Lavacharts
khill.lavacharts.twig: Khill\Lavacharts\Symfony\Bundle\Twig\LavachartsExtension
services:
lavacharts:
class: %khill.lavacharts.class%
lavacharts.twig_extension:
class: %khill.lavacharts.twig%
public: false
arguments: ['@lavacharts']
tags:
- { name: twig.extension }
Installation
============
Step 1: Download the Bundle
---------------------------
Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:
```bash
$ composer require khill/lavacharts "~3.0"
```
This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.
Step 2: Enable the Bundle
-------------------------
Then, enable the bundle by adding it to the list of registered bundles
in the `app/AppKernel.php` file of your project:
```php
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Khill\Lavacharts\Symfony\Bundle\LavachartsBundle(),
);
// ...
}
// ...
}
```
Step 3: Import the Service
-------------------------
Lastly, import the service by adding the `services.yml` to the imports
in the `app/config/config.yml` file of your project:
```php
imports:
// ...
- { resource: "@LavachartsBundle/Resources/config/services.yml" }
```
Step 4: Pull from the Container
-------------------------
Now you can use Lavacharts from within your controller:
```php
$lava = $this->get('lavacharts');
$datatable = $lava->DataTable();
// ...
```
<?php
namespace Khill\Lavacharts\Symfony\Bundle\Twig;
use Khill\Lavacharts\Lavacharts;
class LavachartsExtension extends \Twig_Extension
{
/**
* The Lavacharts object passed in from the service container.
*
* @var \Khill\Lavacharts\Lavacharts
*/
private $lava;
public function __construct($lava)
{
$this->lava = $lava;
}
public function getFunctions()
{
$chartClassesProp = new \ReflectionProperty(Lavacharts::class, 'chartClasses');
$chartClassesProp->setAccessible(true);
$chartClasses = $chartClassesProp->getValue(new Lavacharts);
$renderFunctions = [];
foreach ($chartClasses as $chartClass) {
$renderFunctions[] = new \Twig_SimpleFunction(strtolower($chartClass),
function($chartLabel, $elemId) use ($chartClass) {
return $this->renderChart($chartClass, $chartLabel, $elemId);
}
);
}
return $renderFunctions;
}
public function renderChart($chartType, $chartLabel, $elemId)
{
return $this->lava->render($chartType, $chartLabel, $elemId);
}
public function getName()
{
return 'lavacharts_twig_extension';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment