Skip to content

Commit 3d171ce

Browse files
committed
Merge pull request #6 from takeit/docs
added twig extension
2 parents f9653ee + e6089b2 commit 3d171ce

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Resources/config/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ parameters:
33
takeit_amp_html.amp_controller.class: Takeit\Bundle\AmpHtmlBundle\Controller\AmpViewController
44
takeit_amp_html.routing_loader.class: Takeit\Bundle\AmpHtmlBundle\Routing\Loader\AmpLoader
55
takeit_amp_html.param_converter.resolve_entity_converter.class: Takeit\Bundle\AmpHtmlBundle\Request\ParamConverter\ResolveEntityParamConverter
6+
takeit_amp_html.twig_extension.class: Takeit\Bundle\AmpHtmlBundle\Twig\AmpExtension
67

78
services:
89
takeit_amp_html.amp_subscriber:
@@ -30,3 +31,10 @@ services:
3031
arguments:
3132
- {'Takeit\Bundle\AmpHtmlBundle\Model\AmpInterface': "%takeit_amp_html.configuration.model.class%"}
3233
- "@?doctrine"
34+
35+
takeit_amp_html.twig_extension:
36+
class: "%takeit_amp_html.twig_extension.class%"
37+
public: false
38+
arguments: ['%takeit_amp_html.configuration.routing%']
39+
tags:
40+
- { name: twig.extension }

Twig/AmpExtension.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the takeit/AmpHtmlBundle package.
5+
*
6+
* (c) Rafał Muszyński <rmuszynski1@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Takeit\Bundle\AmpHtmlBundle\Twig;
13+
14+
/**
15+
* AMP Twig Extension.
16+
*/
17+
class AmpExtension extends \Twig_Extension
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $config;
23+
24+
/**
25+
* @param array $config
26+
*/
27+
public function __construct(array $config)
28+
{
29+
$this->config = $config;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getFilters()
36+
{
37+
return array(
38+
new \Twig_SimpleFilter('amp_path', array($this, 'ampFilter')),
39+
);
40+
}
41+
42+
/**
43+
* Returns an AMP path.
44+
*
45+
* @param $path Path
46+
*
47+
* @return string
48+
*/
49+
public function ampFilter($path)
50+
{
51+
return $this->config['prefix'].$path;
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getName()
58+
{
59+
return 'takeit_amp_extension';
60+
}
61+
}

spec/Twig/AmpExtensionSpec.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the takeit/AmpHtmlBundle package.
5+
*
6+
* (c) Rafał Muszyński <rmuszynski1@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace spec\Takeit\Bundle\AmpHtmlBundle\Twig;
13+
14+
use PhpSpec\ObjectBehavior;
15+
use Takeit\Bundle\AmpHtmlBundle\Twig\AmpExtension;
16+
17+
/**
18+
* @mixin AmpExtension
19+
*
20+
* @author Rafał Muszyński <rmuszynski1@gmail.com>
21+
*/
22+
class AmpExtensionSpec extends ObjectBehavior
23+
{
24+
function let()
25+
{
26+
$this->beConstructedWith(['prefix' => '/some/prefix']);
27+
}
28+
29+
function it_is_initializable()
30+
{
31+
$this->shouldHaveType(AmpExtension::class);
32+
}
33+
34+
function it_is_a_twig_extension()
35+
{
36+
$this->shouldHaveType('Twig_Extension');
37+
}
38+
}

0 commit comments

Comments
 (0)