| 1 |
<?php |
| 2 |
|
| 3 |
require_once('lib/generator/common/AbstractSerializer.php'); |
| 4 |
|
| 5 |
abstract class Serializer extends AbstractSerializer |
| 6 |
{ |
| 7 |
private $directory; |
| 8 |
private $suffix; |
| 9 |
|
| 10 |
private $file; |
| 11 |
private $length = 9999; |
| 12 |
|
| 13 |
private $queue; |
| 14 |
private $limit = 50; |
| 15 |
|
| 16 |
public static function clean($directory, $suffix, $filename='provides') { |
| 17 |
$directory .= '/provides'; |
| 18 |
if (!file_exists($directory)) { |
| 19 |
mkdir($directory); |
| 20 |
chmod($directory, 0755); |
| 21 |
} |
| 22 |
$suffix = '.' . $suffix; |
| 23 |
$from = -strlen($suffix); |
| 24 |
foreach (scandir($directory) as $file) { |
| 25 |
if (substr($file, $from) == $suffix) { |
| 26 |
file_exists($directory . '/' . $file) && unlink($directory . '/' . $file); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function __construct($directory, $suffix, $filename='provides') { |
| 32 |
$this->directory = $directory . '/provides'; |
| 33 |
if (!file_exists($this->directory)) { |
| 34 |
mkdir($this->directory); |
| 35 |
chmod($this->directory, 0755); |
| 36 |
} |
| 37 |
$this->suffix = '.' . $suffix; |
| 38 |
} |
| 39 |
|
| 40 |
public function ids() { |
| 41 |
$ids = array(); |
| 42 |
|
| 43 |
$from = -strlen($this->suffix); |
| 44 |
foreach (scandir($this->directory) as $file) { |
| 45 |
if (substr($file, $from) == $this->suffix) { |
| 46 |
$file = fopen($this->directory . '/' . $file, 'r'); |
| 47 |
while (!feof($file)) { |
| 48 |
$line = stream_get_line($file, $this->length, "\n"); |
| 49 |
if ($started && $this->lineEnds($line)) { |
| 50 |
$started = false; |
| 51 |
continue; |
| 52 |
} |
| 53 |
elseif ($id = $this->lineStarts($line)) { |
| 54 |
$started = true; |
| 55 |
$ids[] = $id; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return array_unique($ids); |
| 62 |
} |
| 63 |
|
| 64 |
protected function getString($id, $include_queue = TRUE) { |
| 65 |
return ''; |
| 66 |
} |
| 67 |
|
| 68 |
public function set($id, $value) { |
| 69 |
} |
| 70 |
|
| 71 |
public function setObject($id, $value) { |
| 72 |
$pretty = $this->toAbstract($value, $id); |
| 73 |
$provides = $this->extractProvides($pretty); |
| 74 |
$updates = array(); |
| 75 |
$lookups = array(array('#properties', '#property'), array('#methods', '#method')); |
| 76 |
foreach ($lookups as $list) { |
| 77 |
list($types, $type) = $list; |
| 78 |
if ($pretty[$types]) { |
| 79 |
foreach ($pretty[$types][0][$type] as $item) { |
| 80 |
foreach ($this->extractProvides($item, $provides) as $provide => $item) { |
| 81 |
$updates[$provide][$types][$item['@name']] = $item; |
| 82 |
if (!array_key_exists($provide, $provides)) { |
| 83 |
$provides[$provide] = array(); |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
foreach ($provides as $provide => $provide_value) { |
| 92 |
$file_name = $this->directory . '/' . $provide . $this->suffix; |
| 93 |
|
| 94 |
$found = false; |
| 95 |
$output = implode("\n", $this->header) . "\n"; |
| 96 |
if (file_exists($file_name)) { |
| 97 |
$contents = substr(file_get_contents($file_name), strlen(implode("\n", $this->header))+1, -strlen(implode("\n", $this->footer))-1); |
| 98 |
$buffer = array(); |
| 99 |
foreach (explode("\n", $contents) as $line) { |
| 100 |
if (!empty($buffer) && $this->lineEnds($line)) { |
| 101 |
$buffer[] = $line; |
| 102 |
$found = true; |
| 103 |
$saved = array(); |
| 104 |
$raw = $this->linesToRaw($buffer); |
| 105 |
$object = $this->toObject($raw); |
| 106 |
|
| 107 |
foreach ($lookups as $list) { |
| 108 |
list($types, $type) = $list; |
| 109 |
if ($object[$types]) { |
| 110 |
foreach ($object[$types][0][$type] as $item) { |
| 111 |
$saved[$types][$item['@name']] = $item; |
| 112 |
} |
| 113 |
} |
| 114 |
if (isset($updates[$provide][$types])) { |
| 115 |
foreach ($updates[$provide][$types] as $item) { |
| 116 |
$saved[$types][$item['@name']] = $item; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
foreach ($saved as $types => $item) { |
| 122 |
$provide_value[$types][0][$type][] = $item; |
| 123 |
} |
| 124 |
|
| 125 |
$raw = $this->convertToRaw($provide_value); |
| 126 |
foreach (explode("\n", $this->toString($raw, $id)) as $line) { |
| 127 |
$output .= $this->indent . $line . "\n"; |
| 128 |
} |
| 129 |
|
| 130 |
$buffer = array(); |
| 131 |
} |
| 132 |
elseif ($this->lineStarts($line) == $id) { |
| 133 |
$buffer[] = substr($line, strlen($this->indent)); |
| 134 |
} |
| 135 |
else { |
| 136 |
$output .= $line . "\n"; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (!$found) { |
| 142 |
foreach ($lookups as $list) { |
| 143 |
list($types, $type) = $list; |
| 144 |
if (isset($updates[$provide][$types])) { |
| 145 |
foreach ($updates[$provide][$types] as $item) { |
| 146 |
$provide_value[$types][0][$type][] = $item; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
$raw = $this->convertToRaw($provide_value); |
| 151 |
foreach (explode("\n", $this->toString($raw, $id)) as $line) { |
| 152 |
$output .= $this->indent . $line . "\n"; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$output .= implode("\n", $this->footer); |
| 157 |
file_put_contents($file_name, $output); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
private function extractProvides($value) { |
| 162 |
$provides = array(); |
| 163 |
if ($value['#provides']) { |
| 164 |
foreach ($value['#provides'] as $provide) { |
| 165 |
foreach ($provide['#provide'] as $content) { |
| 166 |
unset($value['#resources']); |
| 167 |
unset($value['#provides']); |
| 168 |
unset($value['#properties']); |
| 169 |
unset($value['#methods']); |
| 170 |
$provides[$content['content']] = $value; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
return $provides; |
| 175 |
} |
| 176 |
} |