Skip to content

Commit 806a7ce

Browse files
authored
Merge pull request #8 from takeit/improvements
Improvements
2 parents 0a9273e + d3270af commit 806a7ce

31 files changed

+670
-439
lines changed

Checker/AmpSupportChecker.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Checker;
13+
14+
final class AmpSupportChecker implements AmpSupportCheckerInterface
15+
{
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function isEnabled()
20+
{
21+
return true;
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\Checker;
13+
14+
interface AmpSupportCheckerInterface
15+
{
16+
/**
17+
* @return bool
18+
*/
19+
public function isEnabled();
20+
}

Controller/AmpViewController.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Takeit\Bundle\AmpHtmlBundle\Controller;
1313

1414
use Symfony\Component\HttpFoundation\Response;
15+
use Takeit\Bundle\AmpHtmlBundle\Converter\AmpConverterInterface;
16+
use Takeit\Bundle\AmpHtmlBundle\Loader\ThemeLoaderInterface;
1517
use Takeit\Bundle\AmpHtmlBundle\Model\AmpInterface;
1618

1719
/**
@@ -27,11 +29,28 @@ class AmpViewController
2729
private $twig;
2830

2931
/**
30-
* @param \Twig_environment $twig
32+
* @var AmpConverterInterface
3133
*/
32-
public function __construct(\Twig_environment $twig)
33-
{
34+
private $converter;
35+
36+
/**
37+
* @var ThemeLoaderInterface
38+
*/
39+
private $themeLoader;
40+
41+
/**
42+
* @param \Twig_Environment $twig
43+
* @param AmpConverterInterface $converter
44+
* @param ThemeLoaderInterface $themeLoader
45+
*/
46+
public function __construct(
47+
\Twig_Environment $twig,
48+
AmpConverterInterface $converter,
49+
ThemeLoaderInterface $themeLoader
50+
) {
3451
$this->twig = $twig;
52+
$this->converter = $converter;
53+
$this->themeLoader = $themeLoader;
3554
}
3655

3756
/**
@@ -41,10 +60,14 @@ public function __construct(\Twig_environment $twig)
4160
*/
4261
public function viewAction(AmpInterface $object)
4362
{
63+
$this->themeLoader->load();
64+
4465
$response = new Response();
45-
$response->setContent($this->twig->render(sprintf('@amp_theme/index.html.twig'), [
66+
$content = $this->twig->render(sprintf('@%s/index.html.twig', ThemeLoaderInterface::THEME_NAMESPACE), [
4667
'object' => $object,
47-
]));
68+
]);
69+
70+
$response->setContent($this->converter->convertToAmp($content));
4871

4972
return $response;
5073
}

DependencyInjection/Compiler/AddTwigPathsPass.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

DependencyInjection/Configuration.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,56 @@ public function getConfigTreeBuilder()
2828
{
2929
$treeBuilder = new TreeBuilder();
3030
$treeBuilder->root('takeit_amp_html')
31+
->canBeDisabled()
32+
->info('Enable or disable Google AMP HTML support.')
3133
->addDefaultsIfNotSet()
3234
->children()
3335
->arrayNode('theme')
34-
->isRequired()
3536
->addDefaultsIfNotSet()
3637
->children()
37-
->scalarNode('themes_path')
38-
->defaultValue('%kernel.root_dir%/Resources/amp')
39-
->end()
40-
->scalarNode('current_theme')
38+
->scalarNode('loader')
39+
->defaultValue('takeit_amp_html.loader.theme.default')
4140
->cannotBeEmpty()
42-
->isRequired()
41+
->end()
42+
->scalarNode('theme_path')
43+
->defaultValue('%kernel.root_dir%/Resources/amp/amp-theme')
4344
->end()
4445
->end()
4546
->end()
47+
->scalarNode('checker')
48+
->defaultValue('takeit_amp_html.checker.default')
49+
->cannotBeEmpty()
50+
->end()
4651
->arrayNode('routing')
4752
->isRequired()
4853
->addDefaultsIfNotSet()
4954
->children()
50-
->scalarNode('controller')
51-
->defaultValue('takeit_amp_html.amp_controller:viewAction')
55+
->arrayNode('route_strategy')
56+
->canBeEnabled()
57+
->addDefaultsIfNotSet()
58+
->children()
59+
->scalarNode('prefix')
60+
->defaultValue('/platform/amp')
61+
->end()
62+
->scalarNode('pattern')
63+
->isRequired()
64+
->cannotBeEmpty()
65+
->end()
66+
->scalarNode('parameter')
67+
->cannotBeEmpty()
68+
->defaultValue('slug')
69+
->end()
70+
->scalarNode('parameterRegex')
71+
->defaultValue('.+')
72+
->info('Parameter\'s regular expression.')
73+
->end()
74+
->end()
5275
->end()
53-
->scalarNode('prefix')
54-
->defaultValue('/platform/amp')
76+
->arrayNode('parameter_strategy')
77+
->canBeEnabled()
5578
->end()
56-
->scalarNode('pattern')
57-
->isRequired()
58-
->cannotBeEmpty()
59-
->end()
60-
->scalarNode('parameter')
61-
->cannotBeEmpty()
62-
->defaultValue('slug')
63-
->end()
64-
->scalarNode('parameterRegex')
65-
->defaultValue('.+')
66-
->info('Parameter\'s regular expression.')
79+
->scalarNode('controller')
80+
->defaultValue('takeit_amp_html.amp_controller:viewAction')
6781
->end()
6882
->end()
6983
->end()

DependencyInjection/TakeitAmpHtmlExtension.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,36 @@ public function load(array $configs, ContainerBuilder $container)
3030
{
3131
$configuration = new Configuration();
3232
$config = $this->processConfiguration($configuration, $configs);
33+
$container->setParameter('takeit_amp_html.configuration.enabled', $config['enabled']);
34+
35+
if (!$config['enabled']) {
36+
return;
37+
}
3338

3439
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3540

41+
if ($config['routing']['parameter_strategy']['enabled']) {
42+
$loader->load('parameter_strategy.yml');
43+
} else {
44+
$container->setParameter($this->getAlias().'.configuration.routing.route_strategy', $config['routing']['route_strategy']);
45+
$loader->load('route_strategy.yml');
46+
}
47+
48+
$container->setAlias('takeit_amp_html.checker', $config['checker']);
3649
$container->setAlias('takeit_amp_html.converter.amp', $config['converter']);
50+
$container->setAlias('takeit_amp_html.loader.theme', $config['theme']['loader']);
3751
$loader->load('converters.yml');
3852
$loader->load('services.yml');
3953

54+
unset($config['theme']['loader']);
4055
foreach ($config['theme'] as $key => $value) {
4156
$container->setParameter(
4257
$this->getAlias().'.configuration.theme.'.$key,
4358
$value
4459
);
4560
}
4661

47-
$container->setParameter(
48-
$this->getAlias().'.configuration.theme.theme_path',
49-
rtrim($config['theme']['themes_path'], '/').'/'.$config['theme']['current_theme']
50-
);
51-
5262
$container->setParameter($this->getAlias().'.configuration.model.class', $config['model']);
53-
$container->setParameter($this->getAlias().'.configuration.routing', $config['routing']);
63+
$container->setParameter($this->getAlias().'.configuration.routing.controller', $config['routing']['controller']);
5464
}
5565
}

EventListener/AmpResponseSubscriber.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)