| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Plugins |
|---|
| 4 | { |
|---|
| 5 | private $dir; |
|---|
| 6 | private static $json; |
|---|
| 7 | private static $formats = array('xml', 'json', 'database'); |
|---|
| 8 | private static $types = array('local', 'remote', 'storage'); |
|---|
| 9 | |
|---|
| 10 | public function __construct($dir) |
|---|
| 11 | { |
|---|
| 12 | $this->dir = $dir; |
|---|
| 13 | $this->json = new Services_JSON(); |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | public function write($by_resource, $by_object) |
|---|
| 17 | { |
|---|
| 18 | $dir = $this->dir . '/plugins/output/'; |
|---|
| 19 | $output_dir = $this->dir . '/output/'; |
|---|
| 20 | $this->delTree($output_dir); |
|---|
| 21 | |
|---|
| 22 | $files = scandir($dir); |
|---|
| 23 | foreach ($files as $file) { |
|---|
| 24 | if (substr($file, -4) == '.php') { |
|---|
| 25 | include_once($dir . $file); |
|---|
| 26 | $name = substr($file, 0, -4); |
|---|
| 27 | foreach (self::$types as $type) { |
|---|
| 28 | foreach (self::$formats as $format) { |
|---|
| 29 | $function = "{$name}_{$type}_{$format}"; |
|---|
| 30 | if (function_exists($function)) { |
|---|
| 31 | if(!file_exists($output_dir)) { |
|---|
| 32 | mkdir($output_dir, 0777); |
|---|
| 33 | } |
|---|
| 34 | if (!file_exists("{$output_dir}{$type}")) { |
|---|
| 35 | mkdir("{$output_dir}{$type}", 0777); |
|---|
| 36 | } |
|---|
| 37 | if (!file_exists("{$output_dir}{$type}/{$format}")) { |
|---|
| 38 | mkdir("{$output_dir}{$type}/{$format}", 0777); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | $data = call_user_func($function, $by_resource, $by_object); |
|---|
| 42 | if(!is_array($data)) continue; |
|---|
| 43 | foreach ($data as $file => $contents) { |
|---|
| 44 | $file = "{$output_dir}{$type}/{$format}/{$file}"; |
|---|
| 45 | if ($format == 'json') { |
|---|
| 46 | file_put_contents($file, $this->json->encode($contents)); |
|---|
| 47 | } |
|---|
| 48 | elseif ($format == 'xml') { |
|---|
| 49 | file_put_contents($file, $contents->saveXML()); |
|---|
| 50 | } |
|---|
| 51 | chmod($file, 0777); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | global $timer; |
|---|
| 55 | $timer->setMarker("$function run"); |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | private function delTree($directory, $last_directory = false) |
|---|
| 64 | { |
|---|
| 65 | if($last_directory){ |
|---|
| 66 | $directory .= '/' . $last_directory; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if (file_exists($directory)) { |
|---|
| 70 | $files = scandir($directory); |
|---|
| 71 | foreach($files as $file){ |
|---|
| 72 | if($file{0} != '.'){ |
|---|
| 73 | if(is_dir($directory . '/' . $file)){ |
|---|
| 74 | $this->delTree($directory, $file); |
|---|
| 75 | if (!in_array('.svn', $files) && scandir($directory . '/' . $file) == 2) { |
|---|
| 76 | rmdir($directory . '/' . $file); |
|---|
| 77 | } |
|---|
| 78 | }else{ |
|---|
| 79 | unlink($directory . '/' . $file); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | ?> |
|---|