-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructure.php
More file actions
150 lines (131 loc) · 4.19 KB
/
Structure.php
File metadata and controls
150 lines (131 loc) · 4.19 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
<?php
declare(strict_types=1);
namespace Effectra\Core;
use Effectra\Core\Exceptions\StructureException;
use Effectra\Fs\Directory;
use Effectra\Fs\Path;
/**
* Class Structure
*
* The Structure class is responsible for scanning and validating the directory structure of an application.
* It ensures that the required directories and subdirectories are present.
*/
class Structure
{
/**
* The main directories that should be present in the application root directory.
*
* @var array
*/
protected static array $main = [
'app', 'bootstrap', 'config', 'database', 'public', 'resources', 'routes', 'storage', 'tests', 'view'
];
/**
* The subdirectories that should be present in the 'app' directory.
*
* @var array
*/
protected static array $appDir = [
'Commands', 'Controllers', 'Middlewares', 'Models', 'Providers'
];
/**
* The subdirectories that should be present in the 'database' directory.
*
* @var array
*/
protected static array $databaseDir = [
'migrations', 'schema'
];
/**
* The subdirectories that should be present in the 'storage' directory.
*
* @var array
*/
protected static array $storageDir = [
'cache', 'exports', 'imports', 'keys', 'logs'
];
/**
* The subdirectories that should be present in the 'resources' directory.
*
* @var array
*/
protected static array $resourcesDir = [
'js', 'css', 'translations'
];
/**
* The directories that are missing in the application structure.
*
* @var array
*/
protected array $missedDir = [];
/**
* Scans the application structure and checks for missing directories.
*
* @return bool True if the application structure is valid, False if there are missing directories.
*/
public function scan(): bool
{
// Scan the main directories
foreach (static::$main as $dir) {
if (!Directory::isDirectory(Application::appPath($dir))) {
$this->missedDir[] = Application::appPath($dir);
}
}
// Scan the 'app' directory subdirectories
foreach (static::$appDir as $dir) {
if (!Directory::isDirectory(Application::appPath('app' . Path::ds() . $dir))) {
$this->missedDir[] = Application::appPath('app' . Path::ds() . $dir);
}
}
// Scan the 'database' directory subdirectories
foreach (static::$databaseDir as $dir) {
if (!Directory::isDirectory(Application::databasePath($dir))) {
$this->missedDir[] = Application::databasePath($dir);
}
}
// Scan the 'storage' directory subdirectories
foreach (static::$storageDir as $dir) {
if (!Directory::isDirectory(Application::storagePath($dir))) {
$this->missedDir[] = Application::storagePath($dir);
}
}
// Scan the 'resources' directory subdirectories
foreach (static::$resourcesDir as $dir) {
if (!Directory::isDirectory(Application::resourcesPath($dir))) {
$this->missedDir[] = Application::resourcesPath($dir);
}
}
if (count($this->missedDir) == 0) {
return true;
}
return false;
}
/**
* Throws a StructureException if a required directory is missing.
*
* @throws StructureException if a required directory is missing
*/
public function declareMissedDirectory(): void
{
$dirs = array_map(fn($dir) => " $dir\n", $this->missedDir);
throw new StructureException("Directory is missing:\n\n" . implode(' ', $dirs));
}
/**
* Builds the missing directories in the application structure.
*/
public function buildFrameworkDirectories(): void
{
foreach ($this->missedDir as $dir) {
Directory::make($dir);
}
}
/**
* Gets the list of directories that are missing in the application structure.
*
* @return array The list of missing directories.
*/
public function getMissedDirectories(): array
{
return $this->missedDir;
}
}