forked from AmastyLtd-zz/Magento-2-Module-Creator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrud.php
More file actions
123 lines (112 loc) · 3.79 KB
/
Crud.php
File metadata and controls
123 lines (112 loc) · 3.79 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
<?php
namespace Amasty;
class Crud
{
protected $requiredFields = ['companyName','moduleName'];
protected $templatePath = 'template/';
protected function getTemplate()
{
$path = realpath($this->templatePath);
$objects = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
return $objects;
}
protected function replace($search, $replace, $subject)
{
$subject = str_replace(
$this->roundArray($search),
array_map('strtolower', $replace),
$subject
);
return str_replace(
$this->roundArray(array_map('ucfirst', $search)),
array_map('ucfirst', $replace),
$subject
);
}
protected function roundArray($array)
{
foreach ($array as $key => $value) {
$array[$key] = '{{'.$value.'}}';
}
return $array;
}
protected function upFirstLetter($array)
{
foreach ($array as $key => $value) {
$array[$key] = ucfirst($value);
}
return $array;
}
protected function replaceParams($params, $objects)
{
$result = [];
foreach ($objects as $name => $object) {
if (is_file($name)) {
$path = explode($this->templatePath, $name);
$content = $this->replace($this->requiredFields, $params, file_get_contents($object));
$path[1] = $this->replace($this->requiredFields, $params, $path[1]);
$object = [
'type'=>'file',
'dirName'=>ucfirst($params['companyName']).'/'.ucfirst($params['moduleName']).'/'.$path[1],
'content'=>$content
];
} else {
$path = explode($this->templatePath, $name);
$path[1] = $this->replace($this->requiredFields, $params, $path[1]);
$object = [
'type'=>'dir',
'dirName'=>ucfirst($params['companyName']).'/'.ucfirst($params['moduleName']).'/'.$path[1]
];
}
$result[] = $object;
}
return $result;
}
protected function archiveFiles($objects)
{
$tmp = tempnam("tmp", "zip");
$archiver = new \ZipArchive();
$archiver->open($tmp, \ZipArchive::OVERWRITE);
foreach ($objects as $object) {
if ($object['type'] == 'file') {
$archiver->addFromString($object['dirName'], $object['content']);
} else {
$archiver->addEmptyDir($object['dirName']);
}
}
$archiver->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($tmp));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($tmp);
unlink($tmp);
}
public function createModule($params)
{
foreach ($this->requiredFields as $required) {
if (!array_key_exists($required, $params) || $params[$required]=='') {
throw new \Exception('Please fill filed '.$required);
}
}
$this->generateArchive($params);
}
protected function prepareParams($params)
{
$search = [' '];
$replace = [''];
foreach ($params as $key => $value) {
$params[$key] = str_replace($search, $replace, $value);
}
return $params;
}
protected function generateArchive($params)
{
$objects = $this->getTemplate();
$params = $this->prepareParams($params);
$objects = $this->replaceParams($params, $objects);
return $this->archiveFiles($objects);
}
}