1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Toggl Dashboard |
5
|
|
|
* |
6
|
|
|
* @category Tollwerk |
7
|
|
|
* @package Tollwerk\Admin |
8
|
|
|
* @subpackage Tollwerk\Admin\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Tollwerk\Admin\Infrastructure; |
38
|
|
|
|
39
|
|
|
use Doctrine\DBAL\Types\Type; |
40
|
|
|
use Doctrine\ORM\EntityManager; |
41
|
|
|
use Doctrine\ORM\Tools\Setup; |
42
|
|
|
use Symfony\Component\Yaml\Yaml; |
43
|
|
|
use Tollwerk\Admin\Application\Contract\PersistenceAdapterFactoryInterface; |
44
|
|
|
use Tollwerk\Admin\Application\Contract\PersistenceServiceInterface; |
45
|
|
|
use Tollwerk\Admin\Application\Contract\StorageAdapterStrategyInterface; |
46
|
|
|
use Tollwerk\Admin\Application\Service\AccountService; |
47
|
|
|
use Tollwerk\Admin\Application\Service\DomainService; |
48
|
|
|
use Tollwerk\Admin\Application\Service\VirtualHostService; |
49
|
|
|
use Tollwerk\Admin\Infrastructure\Doctrine\EnumVhosttypeType; |
50
|
|
|
use Tollwerk\Admin\Infrastructure\Factory\PersistenceAdapterFactory; |
51
|
|
|
use Tollwerk\Admin\Infrastructure\Service\PersistenceService; |
52
|
|
|
use Tollwerk\Admin\Infrastructure\Strategy\DoctrineStorageAdapterStrategy; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* App |
56
|
|
|
* |
57
|
|
|
* @package Tollwerk\Admin |
58
|
|
|
* @subpackage Tollwerk\Admin\Ports |
59
|
|
|
*/ |
60
|
|
|
class App |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* Configuration |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected static $config; |
68
|
|
|
/** |
69
|
|
|
* Root directory |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected static $rootDirectory; |
74
|
|
|
/** |
75
|
|
|
* Entity manager |
76
|
|
|
* |
77
|
|
|
* @var EntityManager |
78
|
|
|
*/ |
79
|
|
|
protected static $entityManager; |
80
|
|
|
/** |
81
|
|
|
* Developer mode |
82
|
|
|
* |
83
|
|
|
* @var boolean |
84
|
|
|
*/ |
85
|
|
|
protected static $devMode; |
86
|
|
|
/** |
87
|
|
|
* Account service |
88
|
|
|
* |
89
|
|
|
* @var AccountService |
90
|
|
|
*/ |
91
|
|
|
protected static $accountService = null; |
92
|
|
|
/** |
93
|
|
|
* Virtual host service |
94
|
|
|
* |
95
|
|
|
* @var VirtualHostService |
96
|
|
|
*/ |
97
|
|
|
protected static $vhostService = null; |
98
|
|
|
/** |
99
|
|
|
* Domain service |
100
|
|
|
* |
101
|
|
|
* @var DomainService |
102
|
|
|
*/ |
103
|
|
|
protected static $domainService = null; |
104
|
|
|
/** |
105
|
|
|
* Active Storage adapter |
106
|
|
|
* |
107
|
|
|
* @var StorageAdapterStrategyInterface |
108
|
|
|
*/ |
109
|
|
|
protected static $storageAdapter; |
110
|
|
|
/** |
111
|
|
|
* Persistence adapter factory |
112
|
|
|
* |
113
|
|
|
* @var PersistenceAdapterFactoryInterface |
114
|
|
|
*/ |
115
|
|
|
protected static $persistenceAdapterFactory; |
116
|
|
|
/** |
117
|
|
|
* Persistence service |
118
|
|
|
* |
119
|
|
|
* @var PersistenceServiceInterface |
120
|
|
|
*/ |
121
|
|
|
protected static $persistenceService; |
122
|
|
|
/** |
123
|
|
|
* App domain |
124
|
|
|
* |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
const DOMAIN = 'admin'; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Bootstrap |
131
|
|
|
* |
132
|
|
|
* @see https://github.com/toggl/toggl_api_docs/blob/master/reports.md |
133
|
|
|
* @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html |
134
|
|
|
* |
135
|
|
|
* @param bool $devMode Developer mode |
136
|
|
|
*/ |
137
|
|
|
public static function bootstrap($devMode = false) |
138
|
|
|
{ |
139
|
|
|
self::$devMode = !!$devMode; |
140
|
|
|
self::$rootDirectory = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR; |
141
|
|
|
|
142
|
|
|
// Initialize the configuration |
143
|
|
|
$config = file_get_contents(self::$rootDirectory.'config'.DIRECTORY_SEPARATOR.'config.yml'); |
144
|
|
|
self::$config = Yaml::parse($config); |
|
|
|
|
145
|
|
|
|
146
|
|
|
// Initialize doctrine |
147
|
|
|
self::initializeDoctrine(); |
148
|
|
|
|
149
|
|
|
// Register the Doctrine storage adapter and persistence adapter factory |
150
|
|
|
self::$storageAdapter = new DoctrineStorageAdapterStrategy(); |
151
|
|
|
self::$persistenceAdapterFactory = new PersistenceAdapterFactory(); |
152
|
|
|
self::$persistenceService = new PersistenceService(self::$persistenceAdapterFactory); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Initialize Doctrine |
157
|
|
|
*/ |
158
|
|
|
protected static function initializeDoctrine() |
159
|
|
|
{ |
160
|
|
|
// If the Doctrine parameters don't exist |
161
|
|
|
if (empty(self::$config['doctrine']) |
162
|
|
|
|| !is_array(self::$config['doctrine']) |
163
|
|
|
|| empty(self::$config['doctrine']['dbparams']) |
164
|
|
|
) { |
165
|
|
|
throw new \InvalidArgumentException('Invalid Doctrine database parameters', 1466175889); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$modelPaths = [ |
169
|
|
|
self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin' |
170
|
|
|
.DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Model' |
171
|
|
|
]; |
172
|
|
|
$dbParams = self::$config['doctrine']['dbparams']; |
173
|
|
|
$config = Setup::createAnnotationMetadataConfiguration($modelPaths, self::$devMode); |
174
|
|
|
|
175
|
|
|
self::$entityManager = EntityManager::create($dbParams, $config); |
176
|
|
|
$platform = self::$entityManager->getConnection()->getDatabasePlatform(); |
177
|
|
|
$platform->registerDoctrineTypeMapping('enum', 'string'); |
178
|
|
|
|
179
|
|
|
// Register the virtual host type declaration |
180
|
|
|
Type::addType(EnumVhosttypeType::ENUM_TYPE, EnumVhosttypeType::class); |
181
|
|
|
|
182
|
|
|
// Set the locale |
183
|
|
|
$locale = self::getConfig('general.locale'); |
184
|
|
|
putenv('LC_ALL='.$locale); |
185
|
|
|
setlocale(LC_ALL, $locale); |
186
|
|
|
|
187
|
|
|
\bindtextdomain(self::DOMAIN, self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin' |
188
|
|
|
.DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Lang'); |
189
|
|
|
\bind_textdomain_codeset(self::DOMAIN, 'UTF-8'); |
190
|
|
|
\textdomain(self::DOMAIN); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return the entity manager |
195
|
|
|
* |
196
|
|
|
* @return EntityManager |
197
|
|
|
*/ |
198
|
|
|
public static function getEntityManager() |
199
|
|
|
{ |
200
|
|
|
return self::$entityManager; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get a configuration value |
205
|
|
|
* |
206
|
|
|
* @param null $key Optional: config value key |
207
|
|
|
* @return mixed Configuration value(s) |
208
|
|
|
*/ |
209
|
3 |
|
public static function getConfig($key = null) |
210
|
|
|
{ |
211
|
3 |
|
if ($key === null) { |
212
|
|
|
return self::$config; |
213
|
|
|
} |
214
|
3 |
|
$keyParts = explode('.', $key); |
215
|
3 |
|
$config =& self::$config; |
216
|
3 |
|
foreach ($keyParts as $keyPart) { |
217
|
3 |
|
if (!array_key_exists($keyPart, $config)) { |
218
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid config key "%s"', $key), 1466179561); |
219
|
|
|
} |
220
|
3 |
|
$config =& $config[$keyPart]; |
221
|
|
|
} |
222
|
3 |
|
return $config; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return the contents of a particular template |
227
|
|
|
* |
228
|
|
|
* @param string $template Template name |
229
|
|
|
* @return string template contents |
230
|
|
|
*/ |
231
|
|
|
public static function getTemplate($template) |
232
|
|
|
{ |
233
|
|
|
$templateFile = self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin' |
234
|
|
|
.DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Templates'.DIRECTORY_SEPARATOR.$template; |
235
|
|
|
if (!file_exists($templateFile)) { |
236
|
|
|
throw new \RuntimeException(sprintf('Unknown template "%s"', $template), 1475503926); |
237
|
|
|
} |
238
|
|
|
return file_get_contents($templateFile); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Return the account service |
243
|
|
|
* |
244
|
|
|
* @return AccountService Account service |
245
|
|
|
*/ |
246
|
|
|
public static function getAccountService() |
247
|
|
|
{ |
248
|
|
|
if (self::$accountService === null) { |
249
|
|
|
self::$accountService = new AccountService(self::$storageAdapter, self::$persistenceService); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return self::$accountService; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Return the virtual host service |
257
|
|
|
* |
258
|
|
|
* @return VirtualHostService Virtual host service |
259
|
|
|
*/ |
260
|
|
|
public static function getVirtualHostService() |
261
|
|
|
{ |
262
|
|
|
if (self::$vhostService === null) { |
263
|
|
|
self::$vhostService = new VirtualHostService(self::$storageAdapter, self::$persistenceService); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return self::$vhostService; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Return the domain service |
271
|
|
|
* |
272
|
|
|
* @return DomainService Domain service |
273
|
|
|
*/ |
274
|
|
|
public static function getDomainService() |
275
|
|
|
{ |
276
|
|
|
if (self::$domainService === null) { |
277
|
|
|
self::$domainService = new DomainService(self::$storageAdapter, self::$persistenceService); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return self::$domainService; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.