Repository navigation
Expand file tree
/
Copy pathFilesystemGenerator.php
More file actions
137 lines (115 loc) · 4.4 KB
/
Copy pathFilesystemGenerator.php
File metadata and controls
137 lines (115 loc) · 4.4 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
<?php namespace RainLab\Builder\Classes;
use Lang;
use File;
use October\Rain\Parse\Bracket as TextParser;
use ApplicationException;
use SystemException;
/**
* Generates filesystem objects basing on a structure provided with an array
* and using file templates and variables. Variables in template files use
* the Twig syntax, but processed with October\Rain\Syntax\Bracket.
*
* Example - generate a plugin directory containing the plugin.php file.
* The file is created from a template, which uses a couple of variables.
*
* $structure = [
* 'author',
* 'author/plugin',
* 'author/plugin/plugin.php' => 'plugin.php.tpl'
* ];
* $generator = new FilesystemGenerator('$', $structure, '$/Author/Plugin/templates/plugin');
*
* $variables = [
* 'namespace' => 'Author/Plugin'
* ];
* $generator->setVariables($variables);
* $generator->generate();
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class FilesystemGenerator
{
protected $destinationPath;
protected $structure;
protected $variables = [];
protected $templatesPath;
/**
* Initializes the object.
* @param string $destinationPath Destination path to create the filesystem objects in.
* The path can contain filesystem symbols.
* @param array $structure Specifies the structure as array.
* @param string $templatesPath Path to the directory that contains file templates.
* The parameter is required only in case any files should be created. The path can
* contain filesystem symbols.
*/
public function __construct($destinationPath, array $structure, $templatesPath = null)
{
$this->destinationPath = File::symbolizePath($destinationPath);
$this->structure = $structure;
if ($templatesPath) {
$this->templatesPath = File::symbolizePath($templatesPath);
}
}
public function setVariables($variables)
{
foreach ($variables as $key => $value) {
$this->setVariable($key, $value);
}
}
public function setVariable($key, $value)
{
$this->variables[$key] = $value;
}
public function generate()
{
if (!File::isDirectory($this->destinationPath)) {
throw new SystemException(Lang::get('rainlab.builder::lang.common.destination_dir_not_exists', ['path'=>$this->destinationPath]));
}
foreach ($this->structure as $key => $value) {
if (is_numeric($key)) {
$this->makeDirectory($value);
}
else {
$this->makeFile($key, $value);
}
}
}
public function getTemplateContents($templateName)
{
$templatePath = $this->templatesPath.DIRECTORY_SEPARATOR.$templateName;
if (!File::isFile($templatePath)) {
throw new SystemException(Lang::get('rainlab.builder::lang.common.template_not_found', ['name'=>$templateName]));
}
$fileContents = File::get($templatePath);
return TextParser::parse($fileContents, $this->variables);
}
protected function makeDirectory($dirPath)
{
$path = $this->destinationPath.DIRECTORY_SEPARATOR.$dirPath;
if (File::isDirectory($path)) {
throw new ApplicationException(Lang::get('rainlab.builder::lang.common.error_dir_exists', ['path'=>$path]));
}
if (!File::makeDirectory($path, 0777, true, true)) {
throw new SystemException(Lang::get('rainlab.builder::lang.common.error_make_dir', ['name'=>$path]));
}
}
protected function makeFile($filePath, $templateName)
{
$path = $this->destinationPath.DIRECTORY_SEPARATOR.$filePath;
if (File::isFile($path)) {
throw new ApplicationException(Lang::get('rainlab.builder::lang.common.error_file_exists', ['path'=>$path]));
}
$fileDirectory = dirname($path);
if (!File::isDirectory($fileDirectory)) {
if (!File::makeDirectory($fileDirectory, 0777, true, true)) {
throw new SystemException(Lang::get('rainlab.builder::lang.common.error_make_dir', ['name'=>$fileDirectory]));
}
}
$fileContents = $this->getTemplateContents($templateName);
if (@File::put($path, $fileContents) === false) {
throw new SystemException(Lang::get('rainlab.builder::lang.common.error_generating_file', ['path'=>$path]));
}
@File::chmod($path);
}
}