|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
namespace JaySDe\HandlebarsBundle\CacheWarmer; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
use JaySDe\HandlebarsBundle\HandlebarsEnvironment; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
|
15
|
|
|
|
|
16
|
|
|
class HandlebarsCacheWarmer implements CacheWarmerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private $container; |
|
19
|
|
|
private $finder; |
|
20
|
|
|
private $logger; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param ContainerInterface $container The dependency injection container |
|
26
|
|
|
* @param TemplateFinderInterface $finder The template paths cache warmer |
|
27
|
|
|
* @param LoggerInterface $logger |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function __construct( |
|
30
|
|
|
ContainerInterface $container, |
|
31
|
|
|
TemplateFinderInterface $finder, |
|
32
|
|
|
LoggerInterface $logger = null |
|
33
|
|
|
){ |
|
34
|
|
|
// We don't inject the HandlebarsEngine directly as it depends on the |
|
35
|
|
|
// template locator (via the loader) which might be a cached one. |
|
36
|
|
|
// The cached template locator is available once the TemplatePathsCacheWarmer |
|
37
|
|
|
// has been warmed up |
|
38
|
3 |
|
$this->container = $container; |
|
39
|
3 |
|
$this->finder = $finder; |
|
40
|
3 |
|
$this->logger = $logger; |
|
41
|
3 |
|
} |
|
42
|
|
|
/** |
|
43
|
|
|
* Warms up the cache. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $cacheDir The cache directory |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function warmUp($cacheDir) |
|
48
|
|
|
{ |
|
49
|
2 |
|
$engine = $this->container->get('handlebars'); |
|
50
|
2 |
|
foreach ($this->finder->findAllTemplates() as $template) { |
|
51
|
2 |
|
if (!in_array($template->get('engine'), ['hbs', 'handlebars'])) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
try { |
|
55
|
2 |
|
$engine->compile($template); |
|
56
|
1 |
|
} catch (\Exception $e) { |
|
57
|
|
|
// problem during compilation, log it and give up |
|
58
|
1 |
|
if ($this->logger instanceof LoggerInterface) { |
|
59
|
1 |
|
$this->logger->warning( |
|
60
|
|
|
sprintf( |
|
61
|
1 |
|
'Failed to compile Handlebars template "%s": "%s"', |
|
62
|
1 |
|
(string) $template, |
|
63
|
2 |
|
$e->getMessage() |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Checks whether this warmer is optional or not. |
|
73
|
|
|
* |
|
74
|
|
|
* @return Boolean always true |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function isOptional() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|