-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathBridge.php
More file actions
174 lines (147 loc) · 4.28 KB
/
Bridge.php
File metadata and controls
174 lines (147 loc) · 4.28 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* This file is part of the TwigBridge package.
*
* @copyright Robert Crowe <hello@vivalacrowe.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TwigBridge;
use Illuminate\Contracts\Container\Container;
use Illuminate\View\ViewFinderInterface;
use InvalidArgumentException;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Loader\LoaderInterface;
/**
* Bridge functions between Laravel & Twig
*/
class Bridge extends Environment
{
/**
* @var string TwigBridge version
*/
const BRIDGE_VERSION = '0.10.0';
/**
* @var \Illuminate\Contracts\Container\Container
*/
protected $app;
/**
* {@inheritdoc}
*/
public function __construct(LoaderInterface $loader, $options = [], Container $app = null)
{
// Twig 2.0 doesn't support `true` anymore
if (isset($options['autoescape']) && $options['autoescape'] === true) {
$options['autoescape'] = 'html';
}
parent::__construct($loader, $options);
$this->app = $app;
}
/**
* Get the Laravel app.
*
* @return \Illuminate\Contracts\Container\Container
*/
public function getApplication()
{
return $this->app;
}
/**
* Set the Laravel app.
*
* @param \Illuminate\Contracts\Container\Container $app
*
* @return void
*/
public function setApplication(Container $app)
{
$this->app = $app;
}
public function loadTemplate($name, $index = null)
{
$template = parent::loadTemplate($name, $index);
$template->setName($this->normalizeName($name));
return $template;
}
/**
* Lint (check) the syntax of a file on the view paths.
*
* @param string $file File to check. Supports dot-syntax.
*
* @return bool Whether the file passed or not.
*/
public function lint($file)
{
$template = $this->app['twig.loader.viewfinder']->getSourceContext($file);
if (!$template) {
throw new InvalidArgumentException('Unable to find file: '.$file);
}
try {
$this->parse($this->tokenize($template, $file));
} catch (Error $e) {
return false;
}
return true;
}
/**
* Merges a context with the shared variables, same as mergeGlobals()
*
* @param array $context An array representing the context
*
* @return array The context merged with the globals
*/
public function mergeShared(array $context)
{
// we don't use array_merge as the context being generally
// bigger than globals, this code is faster.
foreach ($this->app['view']->getShared() as $key => $value) {
if (!array_key_exists($key, $context)) {
$context[$key] = $value;
}
}
return $context;
}
/**
* Merges a context with the defined globals.
*
* @param array $context An array representing the context
*
* @return array The context merged with the globals
*/
public function mergeGlobals(array $context)
{
$context = parent::mergeGlobals($context);
// we don't use array_merge as the context being generally
// bigger than globals, this code is faster.
foreach ($this->app['view']->getShared() as $key => $value) {
if (!array_key_exists($key, $context)) {
$context[$key] = $value;
}
}
return $context;
}
/**
* Normalize a view name.
*
* @param string $name
*
* @return string
*/
protected function normalizeName($name)
{
$extension = '.' . $this->app['twig.extension'];
$length = strlen($extension);
if (substr($name, -$length, $length) === $extension) {
$name = substr($name, 0, -$length);
}
// Normalize namespace and delimiters
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;
if (strpos($name, $delimiter) === false) {
return str_replace('/', '.', $name);
}
list($namespace, $name) = explode($delimiter, $name);
return $namespace.$delimiter.str_replace('/', '.', $name);
}
}