94% of 375OPs |
100% of 67Lines |
79% of 56Branches |
66% of 32Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\scripts\tagger; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum, |
| 7 |
mageekguy\atoum\adapter, |
| 8 |
mageekguy\atoum\exceptions |
| 9 |
; |
| 10 |
|
| 11 |
class engine |
| 12 |
{
|
| 13 |
const defaultVersionPattern = '/\$Rev: [^ %]+ \$/'; |
| 14 |
|
| 15 |
protected $adapter = null; |
| 16 |
protected $version = null; |
| 17 |
protected $versionPattern = null; |
| 18 |
protected $srcDirectory = null; |
| 19 |
protected $srcIteratorInjector = null; |
| 20 |
protected $destinationDirectory = null; |
| 21 |
|
| 22 |
public function __construct(atoum\adapter $adapter = null)100% |
| 23 |
{
|
| 24 |
if ($adapter === null) |
| 25 |
{
|
| 26 |
$adapter = new adapter(); |
| 27 |
} |
| 28 |
|
| 29 |
$this |
| 30 |
->setAdapter($adapter) |
| 31 |
->setVersionPattern(static::defaultVersionPattern) |
| 32 |
; |
| 33 |
} |
| 34 |
|
| 35 |
public function setAdapter(adapter $adapter)100% |
| 36 |
{
|
| 37 |
$this->adapter = $adapter; |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function getAdapter()100% |
| 43 |
{
|
| 44 |
return $this->adapter; |
| 45 |
} |
| 46 |
|
| 47 |
public function getVersion()100% |
| 48 |
{
|
| 49 |
return $this->version; |
| 50 |
} |
| 51 |
|
| 52 |
public function setVersion($version)100% |
| 53 |
{
|
| 54 |
$this->version = (string) $version; |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
public function getVersionPattern()100% |
| 60 |
{
|
| 61 |
return $this->versionPattern; |
| 62 |
} |
| 63 |
|
| 64 |
public function setVersionPattern($pattern)100% |
| 65 |
{
|
| 66 |
$this->versionPattern = (string) $pattern; |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function getSrcDirectory()100% |
| 72 |
{
|
| 73 |
return $this->srcDirectory; |
| 74 |
} |
| 75 |
|
| 76 |
public function setSrcDirectory($directory)100% |
| 77 |
{
|
| 78 |
$this->srcDirectory = rtrim((string) $directory, \DIRECTORY_SEPARATOR); |
| 79 |
|
| 80 |
if ($this->destinationDirectory === null) |
| 81 |
{
|
| 82 |
$this->destinationDirectory = $this->srcDirectory; |
| 83 |
} |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
public function getDestinationDirectory()100% |
| 89 |
{
|
| 90 |
return $this->destinationDirectory; |
| 91 |
} |
| 92 |
|
| 93 |
public function setDestinationDirectory($directory)100% |
| 94 |
{
|
| 95 |
$this->destinationDirectory = rtrim((string) $directory, \DIRECTORY_SEPARATOR); |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
public function setSrcIteratorInjector(\closure $srcIteratorInjector)100% |
| 101 |
{
|
| 102 |
$closure = new \reflectionMethod($srcIteratorInjector, '__invoke'); |
| 103 |
|
| 104 |
if ($closure->getNumberOfParameters() != 1) |
| 105 |
{
|
| 106 |
throw new exceptions\logic('Src iterator injector must take one argument');
|
| 107 |
} |
| 108 |
|
| 109 |
$this->srcIteratorInjector = $srcIteratorInjector; |
| 110 |
|
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
public function getSrcIterator()100% |
| 115 |
{
|
| 116 |
if ($this->srcDirectory === null) |
| 117 |
{
|
| 118 |
throw new exceptions\logic('Unable to get files iterator, source directory is undefined');
|
| 119 |
} |
| 120 |
|
| 121 |
if ($this->srcIteratorInjector === null) |
| 122 |
{
|
| 123 |
$this->setSrcIteratorInjector(function($directory) { return new \recursiveIteratorIterator(new atoum\iterators\filters\recursives\dot($directory)); });
|
| 124 |
} |
| 125 |
|
| 126 |
return $this->srcIteratorInjector->__invoke($this->srcDirectory); |
| 127 |
} |
| 128 |
|
| 129 |
public function tagVersion()100% |
| 130 |
{
|
| 131 |
if ($this->srcDirectory === null) |
| 132 |
{
|
| 133 |
throw new exceptions\logic('Unable to tag, src directory is undefined');
|
| 134 |
} |
| 135 |
|
| 136 |
if ($this->version === null) |
| 137 |
{
|
| 138 |
throw new exceptions\logic('Unable to tag, version is undefined');
|
| 139 |
} |
| 140 |
|
| 141 |
$srcIterator = $this->getSrcIterator(); |
| 142 |
|
| 143 |
if ($srcIterator instanceof \iterator === false) |
| 144 |
{
|
| 145 |
throw new exceptions\logic('Unable to tag, src iterator injector does not return an iterator');
|
| 146 |
} |
| 147 |
|
| 148 |
foreach ($srcIterator as $path) |
| 149 |
{
|
| 150 |
$fileContents = @$this->adapter->file_get_contents($path); |
| 151 |
|
| 152 |
if ($fileContents === false) |
| 153 |
{
|
| 154 |
throw new exceptions\runtime('Unable to tag, path \'' . $path . '\' is unreadable');
|
| 155 |
} |
| 156 |
|
| 157 |
$path = ($this->destinationDirectory == $this->srcDirectory ? $path : $this->destinationDirectory . \DIRECTORY_SEPARATOR . substr($path, strlen($this->srcDirectory) + 1)); |
| 158 |
|
| 159 |
$directory = $this->adapter->dirname($path); |
| 160 |
|
| 161 |
if ($this->adapter->is_dir($directory) === false) |
| 162 |
{
|
| 163 |
$this->adapter->mkdir($directory, 0777, true); |
| 164 |
} |
| 165 |
|
| 166 |
if ($this->adapter->file_put_contents($path, preg_replace($this->versionPattern, $this->version, $fileContents), \LOCK_EX) === false) |
| 167 |
{
|
| 168 |
throw new exceptions\runtime('Unable to tag, path \'' . $path . '\' is unwritable');
|
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $this; |
| 173 |
} |
| 174 |
} |