1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CL\ComposerInit; |
4
|
|
|
|
5
|
|
|
use ZipArchive; |
6
|
|
|
use GuzzleHttp\RequestOptions; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Ivan Kerin <[email protected]> |
11
|
|
|
* @copyright (c) 2014 Clippings Ltd. |
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
13
|
|
|
*/ |
14
|
|
|
class Template |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $values = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ZipArchive |
23
|
|
|
*/ |
24
|
|
|
private $zip; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var resource |
28
|
|
|
*/ |
29
|
|
|
private $zipFile; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $root; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Client |
38
|
|
|
*/ |
39
|
|
|
private $github; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Client $github |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct(Client $github) |
45
|
|
|
{ |
46
|
1 |
|
$this->github = $github; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
public function open($uri) |
50
|
|
|
{ |
51
|
1 |
|
$this->zipFile = tmpfile(); |
52
|
1 |
|
$meta = stream_get_meta_data($this->zipFile); |
53
|
|
|
|
54
|
1 |
|
$this->github->get($uri, [RequestOptions::SINK => $meta['uri']]); |
55
|
|
|
|
56
|
1 |
|
$this->zip = new ZipArchive(); |
57
|
1 |
|
$this->zip->open($meta['uri'], ZIPARCHIVE::CHECKCONS); |
58
|
1 |
|
$this->root = $this->zip->getNameIndex(0); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Client |
63
|
|
|
*/ |
64
|
1 |
|
public function getGithub() |
65
|
|
|
{ |
66
|
1 |
|
return $this->github; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return ZipArchive |
71
|
|
|
*/ |
72
|
1 |
|
public function getZip() |
73
|
|
|
{ |
74
|
1 |
|
return $this->zip; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
public function getRoot() |
81
|
|
|
{ |
82
|
1 |
|
return $this->root; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* set values, convert keys to template kyes |
87
|
|
|
* @param array $values |
88
|
|
|
*/ |
89
|
1 |
|
public function setValues(array $values) |
90
|
|
|
{ |
91
|
1 |
|
$this->values = $values; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
1 |
|
public function getValues() |
100
|
|
|
{ |
101
|
1 |
|
return $this->values; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return contents of prompts.json file |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
1 |
|
public function getPromptNames() |
109
|
|
|
{ |
110
|
1 |
|
return (array) json_decode($this->zip->getFromName($this->root.'prompts.json'), true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Populate content with set template values |
115
|
|
|
* @param string $content |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public function populateValues($content) |
119
|
|
|
{ |
120
|
1 |
|
$values = []; |
121
|
|
|
|
122
|
1 |
|
foreach ($this->values as $key => $value) { |
123
|
1 |
|
$values['{% '.$key.' %}'] = $value; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
1 |
|
return strtr($content, $values); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $filename |
131
|
|
|
* @param string $content |
132
|
|
|
*/ |
133
|
1 |
|
public function putFile($filename, $content) |
134
|
|
|
{ |
135
|
1 |
|
file_put_contents($filename, $content); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $dirname |
140
|
|
|
*/ |
141
|
1 |
|
public function putDir($dirname) |
142
|
|
|
{ |
143
|
1 |
|
if (false === file_exists($dirname)) { |
144
|
1 |
|
mkdir($dirname, 0777); |
145
|
1 |
|
} |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Put all file in "root" dir in the template zip into a given directory |
150
|
|
|
* Replace templates with values |
151
|
|
|
* @param string $dir |
152
|
|
|
*/ |
153
|
1 |
|
public function putInto($dir) |
154
|
|
|
{ |
155
|
1 |
|
for ($i = 0; $i < $this->zip->numFiles; $i++) |
156
|
|
|
{ |
157
|
1 |
|
$name = $this->zip->getNameIndex($i); |
158
|
1 |
|
$rootDir = preg_quote($this->root, '/'); |
159
|
|
|
|
160
|
1 |
|
if (preg_match("/^{$rootDir}root(\/.+)/", $name, $matches)) { |
161
|
1 |
|
$filename = $matches[1]; |
162
|
|
|
|
163
|
1 |
|
if (substr($filename, -1) === '/') { |
164
|
1 |
|
$this->putDir($dir.$filename); |
165
|
1 |
|
} else { |
166
|
1 |
|
$content = $this->populateValues($this->zip->getFromName($name)); |
167
|
1 |
|
$this->putFile($dir.$filename, $content); |
168
|
|
|
} |
169
|
1 |
|
} |
170
|
1 |
|
} |
171
|
1 |
|
} |
172
|
|
|
} |
173
|
|
|
|