|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2017 |
|
4
|
|
|
* |
|
5
|
|
|
* @package Majima |
|
6
|
|
|
* @author David Neustadt <[email protected]> |
|
7
|
|
|
* @copyright 2017 David Neustadt |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Majima\Services; |
|
12
|
|
|
|
|
13
|
|
|
use Dwoo\Core; |
|
14
|
|
|
use Dwoo\Data; |
|
15
|
|
|
use Dwoo\Template\File; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class DwooEngineFactory |
|
21
|
|
|
* @package Majima\Services |
|
22
|
|
|
*/ |
|
23
|
|
|
class DwooEngineFactory extends Core implements DwooEngineFactoryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Container |
|
27
|
|
|
*/ |
|
28
|
|
|
private $container; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ConfigService |
|
32
|
|
|
*/ |
|
33
|
|
|
private $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Data |
|
37
|
|
|
*/ |
|
38
|
|
|
private $templateData; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* DwooEngineFactory constructor. |
|
42
|
|
|
* @param Container $container |
|
43
|
|
|
* @param ConfigService $config |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(Container $container, ConfigService $config) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->container = $container; |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
$this->templateData = new Data(); |
|
50
|
|
|
|
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
|
|
53
|
|
|
$this->addGlobal('service_container', $this->container); |
|
54
|
|
|
$this->setCompileDir($this->container->getParameter('kernel.cache_dir') . DIRECTORY_SEPARATOR . 'templates'); |
|
55
|
|
|
$this->getLoader()->addDirectory(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Dwoo', 'Plugins'])); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Data |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getData() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->templateData; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $data |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setData($data = []) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->templateData->assign($data); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array|string $template |
|
76
|
|
|
* @param array $data |
|
77
|
|
|
* @param int $status |
|
78
|
|
|
* @param string $contentType |
|
79
|
|
|
* @return Response |
|
80
|
|
|
*/ |
|
81
|
|
|
public function render($template = '', $data = [], $status = 200, $contentType = 'text/html') |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->getResponse($template, $data, $status, $contentType); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $template |
|
88
|
|
|
* @param $data |
|
89
|
|
|
* @param $status |
|
90
|
|
|
* @param $contentType |
|
91
|
|
|
* @return Response |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getResponse($template, $data, $status, $contentType) |
|
94
|
|
|
{ |
|
95
|
|
|
// set the base template dir |
|
96
|
|
|
$this->setTemplateDir(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'views'])); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$this->setData($data); |
|
99
|
|
|
|
|
100
|
|
|
$templateFile = new File( |
|
101
|
|
|
$template, |
|
102
|
|
|
null, |
|
103
|
|
|
null, |
|
104
|
|
|
null, |
|
105
|
|
|
$this->getTemplateDir() |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
if (!in_array('parent', array_keys($this->getGlobals()))) { |
|
109
|
|
|
$this->addGlobal('parent', null); |
|
110
|
|
|
} |
|
111
|
|
|
foreach ($this->config->getArray() as $global => $value) { |
|
112
|
|
|
$this->addGlobal($global, $value); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return new Response( |
|
116
|
|
|
$this->get( |
|
117
|
|
|
$templateFile, |
|
118
|
|
|
$this->getData() |
|
119
|
|
|
), |
|
120
|
|
|
$status, |
|
121
|
|
|
['Content-Type' => $contentType] |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |