mageekguy\atoum\scripts\phar\generator: lines coverage

93% of 685

OPs

96% of 124

Lines

71% of 78

Branches

81% of 37

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\scripts\phar\generator::__construct() 18 100% 3 100% 1 0% 1 100%
mageekguy\atoum\scripts\phar\generator::setPharFactory() 12 100% 2 100% 1 100% 1 100%
mageekguy\atoum\scripts\phar\generator::getPharFactory() 6 0% 1 0% 1 0% 1 0%
mageekguy\atoum\scripts\phar\generator::setOriginDirectory() 65 95% 13 100% 13 77% 6 83%
mageekguy\atoum\scripts\phar\generator::getOriginDirectory() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\scripts\phar\generator::setDestinationDirectory() 65 95% 12 100% 13 77% 6 83%
mageekguy\atoum\scripts\phar\generator::setStubFile() 47 96% 9 100% 7 71% 3 100%
mageekguy\atoum\scripts\phar\generator::getDestinationDirectory() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\scripts\phar\generator::getStubFile() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\scripts\phar\generator::doRun() 382 92% 46 98% 28 64% 10 90%
mageekguy\atoum\scripts\phar\generator::cleanPath() 38 92% 10 80% 10 70% 5 40%
mageekguy\atoum\scripts\phar\generator::setArgumentHandlers() 34 100% 25 96% 1 100% 1 100%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\scripts\phar;
4

                    
5
require_once __DIR__ . '/../../../constants.php';
6

                    
7
use
8
	mageekguy\atoum,
9
	mageekguy\atoum\iterators,
10
	mageekguy\atoum\exceptions
11
;
12

                    
13
class generator extends atoum\script
14
{
15
	const phar = 'atoum.phar';
16

                    
17
	protected $originDirectory = null;
18
	protected $destinationDirectory = null;
19
	protected $stubFile = null;
20
	protected $pharFactory = null;
21

                    
22
	public function __construct($name, atoum\adapter $adapter = null)100%
23
	{
24
		parent::__construct($name, $adapter);
25

                    
26
		$this->setPharFactory();
27
	}
28

                    
29
	public function setPharFactory(\closure $factory = null)100%
30
	{
31
		$this->pharFactory = $factory ?: function($path) { return new \phar($path); };
32

                    
33
		return $this;
34
	}
35

                    
36
	public function getPharFactory()0%
37
	{
38
		return $this->pharFactory;
39
	}
40

                    
41
	public function setOriginDirectory($directory)100%
42
	{
43
		$originDirectory = $this->cleanPath($directory);
44

                    
45
		if ($originDirectory == '')
46
		{
47
			throw new exceptions\runtime('Empty origin directory is invalid');
48
		}
49
		else if ($this->adapter->is_dir($originDirectory) === false)
50
		{
51
			throw new exceptions\runtime('Path \'' . $originDirectory . '\' of origin directory is invalid');
52
		}
53
		else if ($this->destinationDirectory !== null && $originDirectory === $this->destinationDirectory)
54
		{
55
			throw new exceptions\runtime('Origin directory must be different from destination directory');
56
		}
57

                    
58
		$this->originDirectory = $originDirectory;
59

                    
60
		return $this;
61
	}
62

                    
63
	public function getOriginDirectory()100%
64
	{
65
		return $this->originDirectory;
66
	}
67

                    
68
	public function setDestinationDirectory($directory)100%
69
	{
70
		$destinationDirectory = $this->cleanPath($directory);
71

                    
72
		if ($destinationDirectory == '')
73
		{
74
			throw new exceptions\runtime('Empty destination directory is invalid');
75
		}
76
		else if ($this->adapter->is_dir($destinationDirectory) === false)
77
		{
78
			throw new exceptions\runtime('Path \'' . $destinationDirectory . '\' of destination directory is invalid');
79
		}
80
		else if ($this->originDirectory !== null && $destinationDirectory === $this->originDirectory)
81
		{
82
			throw new exceptions\runtime('Destination directory must be different from origin directory');
83
		}
84

                    
85
		$this->destinationDirectory = $destinationDirectory;
86

                    
87
		return $this;
88
	}
89

                    
90
	public function setStubFile($stubFile)100%
91
	{
92
		$stubFile = $this->cleanPath($stubFile);
93

                    
94
		if ($stubFile == '')
95
		{
96
			throw new exceptions\runtime('Stub file is invalid');
97
		}
98

                    
99
		if ($this->adapter->is_file($stubFile) === false)
100
		{
101
			throw new exceptions\runtime('Stub file is not a valid file');
102
		}
103

                    
104
		$this->stubFile = $stubFile;
105

                    
106
		return $this;
107
	}
108

                    
109
	public function getDestinationDirectory()100%
110
	{
111
		return $this->destinationDirectory;
112
	}
113

                    
114
	public function getStubFile()100%
115
	{
116
		return $this->stubFile;
117
	}
118

                    
119
	protected function doRun()98%
120
	{
121
		if ($this->originDirectory === null)
122
		{
123
			throw new exceptions\runtime(sprintf($this->locale->_('Origin directory must be defined'), $this->originDirectory));
124
		}
125

                    
126
		if ($this->destinationDirectory === null)
127
		{
128
			throw new exceptions\runtime(sprintf($this->locale->_('Destination directory must be defined'), $this->originDirectory));
129
		}
130

                    
131
		if ($this->stubFile === null)
132
		{
133
			throw new exceptions\runtime(sprintf($this->locale->_('Stub file must be defined'), $this->originDirectory));
134
		}
135

                    
136
		if ($this->adapter->is_readable($this->originDirectory) === false)
137
		{
138
			throw new exceptions\runtime(sprintf($this->locale->_('Origin directory \'%s\' is not readable'), $this->originDirectory));
139
		}
140

                    
141
		if ($this->adapter->is_writable($this->destinationDirectory) === false)
142
		{
143
			throw new exceptions\runtime(sprintf($this->locale->_('Destination directory \'%s\' is not writable'), $this->destinationDirectory));
144
		}
145

                    
146
		if ($this->adapter->is_readable($this->stubFile) === false)
147
		{
148
			throw new exceptions\runtime(sprintf($this->locale->_('Stub file \'%s\' is not readable'), $this->stubFile));
149
		}
150

                    
151
		$pharFile = $this->destinationDirectory . DIRECTORY_SEPARATOR . self::phar;
152

                    
153
		@$this->adapter->unlink($pharFile);
154

                    
155
		$description = @$this->adapter->file_get_contents($this->originDirectory . DIRECTORY_SEPARATOR . 'ABOUT');
156

                    
157
		if ($description === false)
158
		{
159
			throw new exceptions\runtime(sprintf($this->locale->_('ABOUT file is missing in \'%s\''), $this->originDirectory));
160
		}
161

                    
162
		$licence = @$this->adapter->file_get_contents($this->originDirectory . DIRECTORY_SEPARATOR . 'COPYING');
163

                    
164
		if ($licence === false)
165
		{
166
			throw new exceptions\runtime(sprintf($this->locale->_('COPYING file is missing in \'%s\''), $this->originDirectory));
167
		}
168

                    
169
		$stub = @$this->adapter->file_get_contents($this->stubFile);
170

                    
171
		if ($stub === false)
172
		{
173
			throw new exceptions\runtime(sprintf($this->locale->_('Unable to read stub file \'%s\''), $this->stubFile));
174
		}
175

                    
176
		$phar = call_user_func($this->pharFactory, $pharFile);
177

                    
178
		$phar['versions'] = serialize(array('1' => atoum\version, 'current' => '1'));
179

                    
180
		$phar->setStub($stub);
181
		$phar->setMetadata(
182
			array(
183
				'version' => atoum\version,
184
				'author' => atoum\author,
185
				'support' => atoum\mail,
186
				'repository' => atoum\repository,
187
				'description' => $description,
188
				'licence' => $licence
189
			)
190
		);
191

                    
192
		$phar->buildFromIterator(new iterators\recursives\atoum\source($this->originDirectory, '1'));
193
		$phar->setSignatureAlgorithm(\phar::SHA1);
194

                    
195
		return $this;
196
	}
197

                    
198
	protected function cleanPath($path)80%
199
	{
200
		$path = $this->adapter->realpath((string) $path);
201

                    
202
		if ($path === false)
203
		{
204
			$path = '';
205
		}
206
		else if (DIRECTORY_SEPARATOR != '/' || $path != '/')
207
		{
208
			$path = rtrim($path, DIRECTORY_SEPARATOR);
209
		}
210

                    
211
		return $path;
212
	}
213

                    
214
	protected function setArgumentHandlers()96%
215
	{
216
		return $this
217
			->addArgumentHandler(
218
				function($script, $argument, $values) {
219
					if (sizeof($values) !== 0)
220
					{
221
						throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName()));
222
					}
223

                    
224
					$script->help();
225
				},
226
				array('-h', '--help'),
227
				null,
228
				'Display this help'
229
			)
230
			->addArgumentHandler(
231
				function($script, $argument, $values) {
232
					if (sizeof($values) !== 1)
233
					{
234
						throw new exceptions\logic\invalidArgument(sprintf($script->getLocale()->_('Bad usage of %s, do php %s --help for more informations'), $argument, $script->getName()));
235
					}
236

                    
237
					$script->setDestinationDirectory($values[0]);
238
				},
239
				array('-d', '--directory'),
240
				'<directory>',
241
				$this->locale->_('Destination directory <dir>')
242
			)
243
		;
244
	}
245
}