Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lebris committed Feb 28, 2015
0 parents commit 9fcc007
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.buildpath
.project
.settings
composer.phar
composer.lock
vendor
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Twig service provider

A silex provider for Twig using puzzle/configuration (https://github.com/Niktux/puzzle-configuration)

## Installation

```
{
"require": {
"silex-spear/twig-provider": "~1.0"
}
}
```

or

```bash
composer require silex-spear/twig-provider
```

#### What is the `config/twig.yml-dist.example` file ?

It's a configuration example file for the configuration file hydrator karma (https://github.com/Niktux/karma)
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "silex-spear/twig-provider",
"description": "A Silex provider for Twig",
"type": "library",
"authors": [{
"name": "Claude LE BRIS",
"email": "[email protected]"
}],
"license": [
"MIT"
],
"require": {
"php": ">=5.4",
"silex/silex": "~1.2",
"puzzle/configuration": "~1.7",
"twig/twig": "~1.18"
},
"autoload": {
"psr-4": {
"Spear\\Silex\\Provider\\": "src/"
}
}
}
3 changes: 3 additions & 0 deletions config/twig.yml-dist.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
developmentMode: <%twig.developmentMode%>
cache:
directory: <%twig.cache.directory%>
90 changes: 90 additions & 0 deletions src/Twig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Spear\Silex\Provider;

use Silex\ServiceProviderInterface;
use Puzzle\Configuration;
use Silex\Application;
use Silex\Provider\DoctrineServiceProvider;
use Silex\Provider\TwigServiceProvider;

class Twig implements ServiceProviderInterface
{
public function register(Application $app)
{
$this->app = $app;
$this->validatePuzzleConfiguration($app);
$this->initializeTwigProvider($app);
}

public function addPath($paths, $prioritary = true)
{
if(! is_array($paths))
{
$paths = array($paths);
}

$twigPath = $this->retrieveExistingTwigPath();

$arrayAddFunction = 'array_push';
if($prioritary === true)
{
$arrayAddFunction = 'array_unshift';
$paths = array_reverse($paths);
}

$this->app['twig.path'] = $this->app->share(function() use($twigPath, $paths, $arrayAddFunction) {
foreach ($paths as $path)
{
$arrayAddFunction($twigPath, $path);
}

return $twigPath;
});
}

public function boot(Application $app)
{
}

private function initializeTwigProvider(Application $app)
{
$app->register(new TwigServiceProvider());

$app['twig.cache.path'] = $app['var.path'] . $app['configuration']->read('twig/cache/directory', false);

$app['twig.options'] = array(
'cache' => $app['twig.cache.path'],
'auto_reload' => $app['configuration']->read('twig/developmentMode', false),
);

$app['twig.path.manager'] = function ($c) {
return $this;
};
}

private function retrieveExistingTwigPath()
{
$path = array();

if(isset($this->app['twig.path']))
{
$path = $this->app['twig.path'];

if($path instanceof \Closure)
{
$path = $path();
}
}

return $path;
}

private function validatePuzzleConfiguration(Application $app)
{
if(! isset($app['configuration']) || ! $app['configuration'] instanceof Configuration)
{
throw new \LogicException('AsseticProvider requires an instance of puzzle/configuration for the key configuration to be defined.');
}
}
}

0 comments on commit 9fcc007

Please sign in to comment.