Skip to content

Commit

Permalink
Merge branch 'koriym-qualifier'
Browse files Browse the repository at this point in the history
  • Loading branch information
madapaja committed Feb 16, 2015
2 parents 653499b + 5d953fb commit 771873c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,34 @@ For example you are using [BEAR.Package](https://github.com/koriym/BEAR.Package)
modifying your `AppModule` as:

```php
namespace YourVendor\YourApp\Module;
namespace MyVendor\MyPackage\Module;

use Madapaja\TwigModule\Annotation\TwigOptions;
use Madapaja\TwigModule\Annotation\TwigPaths;
use Madapaja\TwigModule\TwigModule;
use Ray\Di\AbstractModule;

class AppModule extends AbstractModule
{
protected function configure()
{
$appDir = dirname(dirname(__DIR__));
$this->install(new TwigModule([$appDir . '/src/Resource']));
$this->install(new TwigModule());

// You can add twig template paths by the following
// $this->install(new TwigModule([$appDir . '/src/Resource', $appDir . '/var/lib/twig']));

// Or you can set environment options by 2nd argument
// see: http://twig.sensiolabs.org/doc/api.html#environment-options
// $this->install(new TwigModule([$appDir . '/src/Resource'], [
// 'debug' => true,
// 'cache' => '/tmp/'
// ]));

// (Existing configurations here)
$appDir = dirname(dirname(__DIR__));
$paths = [$appDir . '/src/Resource', $appDir . '/var/lib/twig'];
$this->bind()->annotatedWith(TwigPaths::class)->toInstance($paths);

// Also you can set environment options
// @see http://twig.sensiolabs.org/doc/api.html#environment-options
$options = [
'debug' => true,
'cache' => '/tmp/'
];
$this->bind()->annotatedWith(TwigOptions::class)->toInstance($options);
}
}

```

And you put twig template file into the resource directory.
Expand Down
15 changes: 15 additions & 0 deletions src/Annotation/TwigOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Madapaja\TwigModule\Annotation;

use Ray\Di\Di\Qualifier;

/**
* @Annotation
* @Target("METHOD")
* @Qualifier
*/
final class TwigOptions
{
public $value;
}
15 changes: 15 additions & 0 deletions src/Annotation/TwigPaths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Madapaja\TwigModule\Annotation;

use Ray\Di\Di\Qualifier;

/**
* @Annotation
* @Target("METHOD")
* @Qualifier
*/
final class TwigPaths
{
public $value;
}
4 changes: 4 additions & 0 deletions src/DoctrineAnnotations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

require_once __DIR__ . '/Annotation/TwigOptions.php';
require_once __DIR__ . '/Annotation/TwigPaths.php';
14 changes: 10 additions & 4 deletions src/TwigModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Madapaja\TwigModule;

use BEAR\Resource\RenderInterface;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Madapaja\TwigModule\Annotation\TwigOptions;
use Madapaja\TwigModule\Annotation\TwigPaths;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
use Twig_Environment;
Expand Down Expand Up @@ -38,21 +41,24 @@ public function __construct($paths = [], $options = [])
*/
protected function configure()
{
AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php');
$this->bind(RenderInterface::class)->to(TwigRenderer::class);
$this->bind()->annotatedWith('twig_paths')->toInstance($this->paths);
$this->bind()->annotatedWith('twig_options')->toInstance($this->options);
if ($this->paths) {
$this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
$this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
}
$this
->bind(Twig_LoaderInterface::class)
->annotatedWith('twig_loader')
->toConstructor(
Twig_Loader_Filesystem::class,
'paths=twig_paths'
'paths=Madapaja\TwigModule\Annotation\TwigPaths'
);
$this
->bind(Twig_Environment::class)
->toConstructor(
Twig_Environment::class,
'loader=twig_loader,options=twig_options'
'loader=twig_loader,options=Madapaja\TwigModule\Annotation\TwigOptions'
)
->in(Scope::SINGLETON);
}
Expand Down
2 changes: 1 addition & 1 deletion src/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function getReflection(ResourceObject $ro)
}

/**
* return templete file full path
* return template file full path
*
* @param ResourceObject $ro
* @return string
Expand Down

0 comments on commit 771873c

Please sign in to comment.