/
Blade.php
123 lines (95 loc) · 3.41 KB
/
Blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Rougin\Weasley\Packages\Laravel;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\Configuration;
use Rougin\Slytherin\Integration\IntegrationInterface;
use Rougin\Weasley\Renderers\BladeRenderer;
/**
* Laravel Blade Integration
*
* An integration for the Laravel Blade package (illuminate/view).
*
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Blade implements IntegrationInterface
{
const RENDERER = 'Rougin\Slytherin\Template\RendererInterface';
const VIEW_FACTORY = 'Illuminate\Contracts\View\Factory';
/**
* @var string
*/
protected $interface = 'Rougin\Slytherin\Template\RendererInterface';
/**
* Defines the specified integration.
*
* @param \Rougin\Slytherin\Container\ContainerInterface $container
* @param \Rougin\Slytherin\Integration\Configuration $config
* @return \Rougin\Slytherin\Container\ContainerInterface
*/
public function define(ContainerInterface $container, Configuration $config)
{
$filesystem = new Filesystem;
$result = $this->locations($config);
/** @var string */
$compiled = $result['compiled'];
/** @var string[] */
$templates = $result['templates'];
// Defines the view factory ----------------------------
$resolver = $this->resolver($compiled, $filesystem);
$finder = new FileViewFinder($filesystem, $templates);
$dispatcher = new Dispatcher(new Container);
$factory = new Factory($resolver, $finder, $dispatcher);
// -----------------------------------------------------
$container->set(self::VIEW_FACTORY, $factory);
$renderer = new BladeRenderer($factory);
return $container->set(self::RENDERER, $renderer);
}
/**
* Returns the EngineResolver instance.
*
* @param string $compiled
* @param \Illuminate\Filesystem\Filesystem $filesystem
* @return \Illuminate\View\Engines\EngineResolver
*/
protected function resolver($compiled, $filesystem)
{
$resolver = new EngineResolver;
$callback = function () use ($compiled, $filesystem)
{
$blade = new BladeCompiler($filesystem, $compiled);
return new CompilerEngine($blade);
};
$resolver->register('blade', $callback);
return $resolver;
}
/**
* Returns the compiled and template locations.
*
* @param \Rougin\Slytherin\Integration\Configuration $config
* @return array<string, string[]|string>
*/
protected function locations(Configuration $config)
{
$view = 'illuminate.view';
/** @var string|string[] */
$templates = $config->get($view . '.templates', array());
/** @var string */
$compiled = $config->get($view . '.compiled', '');
if (is_string($templates))
{
$templates = array($templates);
}
$result = array('compiled' => $compiled);
$result['templates'] = $templates;
return (array) $result;
}
}