-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9fcc007
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.buildpath | ||
.project | ||
.settings | ||
composer.phar | ||
composer.lock | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
developmentMode: <%twig.developmentMode%> | ||
cache: | ||
directory: <%twig.cache.directory%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} |