forked from Contentify/Contentify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppBridge.php
192 lines (167 loc) · 5.05 KB
/
AppBridge.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
// phpcs:disable PSR1.Classes.ClassDeclaration --pre-app install usage
// Require Composer autoloader.
// Note that this means that we have access to Laravel and Symfony components!
$autoloaderFile = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoloaderFile)) {
require $autoloaderFile;
} else {
throw new \Exception(
'Error: Could not find autoloader file "'.$autoloaderFile.'". Did you run "composer install"?'
);
}
// Require Laravel's helpers file with useful helper functions
require __DIR__.'/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php';
require __DIR__.'/vendor/laravel/framework/src/Illuminate/Support/helpers.php';
/**
* This class tries to give the (attempt of an) answer to the question:
* How can we use parts of Contentify *outside* of the actual Contentify
* application, especially when it is not yet installed?
* This script & class allows us to access some(!) parts of Contentify
* from the outside.
* ATTENTION: There is no "safe sandbox" - there is no protection against
* use/calls of classes and functions that do not work outside Contentify.
* So if you use this class, better know what you are doing.
*/
class AppBridge
{
/**
* Path of the application directory
*
* @var string
*/
protected $appDir = __DIR__.'/';
/**
* Path of the config directory
*
* @var string
*/
protected $configDir = '';
/**
* Path of the storage directory
*
* @var string
*/
protected $storageDir = '';
/**
* Array that stores the configs so we do not have
* to reload them for each config access
*
* @var array
*/
protected $configs = [];
public function __construct()
{
$this->configDir = $this->appDir.'config/';
$this->storageDir = $this->appDir.'storage/';
}
/**
* Returns the path to the app directory
*
* @return string
*/
public function getAppDir() : string
{
return $this->appDir;
}
/**
* Returns the path to the config directory
*
* @return string
*/
public function getConfigDir() : string
{
return $this->configDir;
}
/**
* Returns the path to the storage directory
*
* @return string
*/
public function getStorageDir() : string
{
return $this->storageDir;
}
/**
* Loads and returns the values of a config file.
* Does not check if the file is a valid config file!
* Use getConfig() if you do not want to enforce reloading.
*
* @param string $name The (file) name of the config; without path and extension
* @return array[]
*/
public function loadConfig(string $name)
{
$config = include $this->getConfigDir().$name.'.php';
return $config;
}
/**
* Returns the values of a config file.
* Uses in-memory caching so it does not
* read the file for each request.
*
* @param string $name The (file) name of the config; without path and extension
* @return array[]
*/
public function getConfig(string $name) : array
{
if (array_key_exists($name, $this->configs)) {
return $this->configs[$name];
} else {
$config = $this->loadConfig($name);
$this->configs[$name] = $config;
return $config;
}
}
/**
* Returns the part of the database config that contains
* the connection details of a specific connection.
*
* @param string $connection Key/name of the connection. Empty = use default
* @return array
*/
public function getDatabaseConnectionDetails(string $connection = '') : array
{
$config = $this->getConfig('database');
if (! $connection) {
$connection = $config['default'];
}
return $config['connections'][$connection];
}
/**
* Creates a new database connection object and returns it
*
* @return \PDO
*/
public function createDatabaseConnection() : PDO
{
$configFile = $this->storageDir.'app/database.ini';
$settings = parse_ini_file($configFile);
// This will only work for MySQL database. If we ever want to support
// more than just MySQL we have to adapt this code.
// (Unfortunately we cannot use Laravel's ConnectionFactory class easily.)
$driver = $this->getDatabaseConnectionDetails()['driver'];
$dsn = $driver.':dbname='.$settings['database'].';host='.$settings['host'];
$pdo = new PDO($dsn, $settings['username'], $settings['password']);
return $pdo;
}
/**
* Returns true if the application is installed
*
* @return bool
*/
public function isAppInstalled() : bool
{
$filename = $this->storageDir.'app/.installed';
return file_exists(__DIR__.'/../'.$filename);
}
/**
* Determine if PHP is being run from the CLI
*
* @return bool
*/
public function isCli() : bool
{
return (php_sapi_name() === 'cli');
}
}