-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathPhar.php
More file actions
186 lines (158 loc) · 3.61 KB
/
Copy pathPhar.php
File metadata and controls
186 lines (158 loc) · 3.61 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
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
<?php
namespace PHPCensor\Plugin;
use Exception;
use Phar as PHPPhar;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Phar Plugin
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Phar extends Plugin
{
/**
* Phar Filename
* @var string
*/
protected $filename;
/**
* Regular Expression Filename Capture
* @var string
*/
protected $regexp;
/**
* Stub Filename
* @var string
*/
protected $stub;
/**
* @return string
*/
public static function pluginName()
{
return 'phar';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
// Filename?
if (isset($options['filename'])) {
$this->setFilename($options['filename']);
}
// RegExp?
if (isset($options['regexp'])) {
$this->setRegExp($options['regexp']);
}
// Stub?
if (isset($options['stub'])) {
$this->setStub($options['stub']);
}
}
/**
* Filename Setter
*
* @param string $filename Configuration Value
* @return Phar Fluent Interface
*/
public function setFilename($filename)
{
$this->filename = $filename;
return $this;
}
/**
* Filename Getter
*
* @return string Configurated or Default Value
*/
public function getFilename()
{
if (!isset($this->filename)) {
$this->setFilename('build.phar');
}
return $this->filename;
}
/**
* Regular Expression Setter
*
* @param string $regexp Configuration Value
* @return Phar Fluent Interface
*/
public function setRegExp($regexp)
{
$this->regexp = $regexp;
return $this;
}
/**
* Regular Expression Getter
*
* @return string Configurated or Default Value
*/
public function getRegExp()
{
if (!isset($this->regexp)) {
$this->setRegExp('/\.php$/');
}
return $this->regexp;
}
/**
* Stub Filename Setter
*
* @param string $stub Configuration Value
* @return Phar Fluent Interface
*/
public function setStub($stub)
{
$this->stub = $stub;
return $this;
}
/**
* Stub Filename Getter
*
* @return string Configurated Value
*/
public function getStub()
{
return $this->stub;
}
/**
* Get stub content for the Phar file.
* @return string
*/
public function getStubContent()
{
$content = '';
$filename = $this->getStub();
if ($filename) {
$content = \file_get_contents($this->builder->buildPath . $this->getStub());
}
return $content;
}
/**
* Run the phar plugin.
* @return bool
*/
public function execute()
{
$success = false;
try {
$phar = new PHPPhar($this->directory . $this->getFilename(), 0, $this->getFilename());
$phar->buildFromDirectory($this->builder->buildPath, $this->getRegExp());
$stub = $this->getStubContent();
if ($stub) {
$phar->setStub($stub);
}
$success = true;
} catch (\Throwable $e) {
$this->builder->logFailure('Phar Plugin Internal Error. Exception: ' . $e->getMessage());
}
return $success;
}
}