|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the doyo/mezzio-testing project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Doyo\Mezzio\Testing\Modules; |
|
15
|
|
|
|
|
16
|
|
|
use Doyo\Mezzio\Testing\Exception\WithConfigurationException; |
|
17
|
|
|
use Laminas\ConfigAggregator\ArrayProvider; |
|
18
|
|
|
use Psr\Container\ContainerInterface; |
|
19
|
|
|
|
|
20
|
|
|
trait WithConfigurationFile |
|
21
|
|
|
{ |
|
22
|
|
|
protected static ?string $configDir = null; |
|
23
|
|
|
|
|
24
|
|
|
protected static string $configFilename = 'config.php'; |
|
25
|
|
|
protected static string $containerFilename = 'container.php'; |
|
26
|
|
|
protected static string $pipelineFilename = 'pipeline.php'; |
|
27
|
|
|
protected static string $routesFilename = 'routes.php'; |
|
28
|
|
|
|
|
29
|
|
|
abstract protected function setupConfigFiles(); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @throws WithConfigurationException when config directory not exists |
|
33
|
|
|
* @throws WithConfigurationException when configuration file not exists |
|
34
|
|
|
*/ |
|
35
|
|
|
final protected static function loadConfigFromFile(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$configDir = static::$configDir; |
|
38
|
|
|
|
|
39
|
|
|
if ( ! is_dir($configDir)) { |
|
40
|
|
|
throw WithConfigurationException::configDirNotExists($configDir); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$configFile = $configDir.'/'.static::$configFilename; |
|
44
|
|
|
if ( ! is_file($configFile) || ! is_readable($configFile)) { |
|
45
|
|
|
throw WithConfigurationException::configFileNotExists($configDir, static::$configFilename); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// reset default config providers |
|
49
|
|
|
static::$configProviders = []; |
|
|
|
|
|
|
50
|
|
|
$configs = require $configFile; |
|
51
|
|
|
$provider = new ArrayProvider($configs); |
|
52
|
|
|
static::addConfigProvider($provider); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws WithConfigurationException @throws when container configuration not exists |
|
57
|
|
|
* |
|
58
|
|
|
* @return ContainerInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
final protected static function loadContainerFromFile(): ContainerInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$containerFile = static::$configDir.'/'.static::$containerFilename; |
|
63
|
|
|
|
|
64
|
|
|
if ( ! is_file($containerFile) || ! is_readable($containerFile)) { |
|
65
|
|
|
throw WithConfigurationException::containerFileNotExist(static::$configDir, static::$containerFilename); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return require $containerFile; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws WithConfigurationException when pipeline configuration not exists |
|
73
|
|
|
*/ |
|
74
|
|
|
final protected static function loadPipelineFromFile(): callable |
|
75
|
|
|
{ |
|
76
|
|
|
$pipelineFile = static::$configDir.'/'.static::$pipelineFilename; |
|
77
|
|
|
|
|
78
|
|
|
if ( ! is_file($pipelineFile) || ! is_readable($pipelineFile)) { |
|
79
|
|
|
throw WithConfigurationException::pipelineFileNotExists(static::$configDir, static::$pipelineFilename); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return require $pipelineFile; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws WithConfigurationException when routes configuration file not exists |
|
87
|
|
|
*/ |
|
88
|
|
|
final protected static function loadRoutesFromFile(): callable |
|
89
|
|
|
{ |
|
90
|
|
|
$routesFile = static::$configDir.'/'.static::$routesFilename; |
|
91
|
|
|
|
|
92
|
|
|
if ( ! is_file($routesFile) || ! is_readable($routesFile)) { |
|
93
|
|
|
throw WithConfigurationException::routesFileNotExists(static::$configDir, static::$routesFilename); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return require $routesFile; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|