| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disllow direct access. |
| 7 |
defined('ABSPATH') or die('Access denied'); |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* |
| 12 |
*/ |
| 13 |
class CJTPackageFile { |
| 14 |
|
| 15 |
/** |
| 16 |
* |
| 17 |
*/ |
| 18 |
const DEFINITION_FILE_NAME = 'definition.xml'; |
| 19 |
|
| 20 |
/** |
| 21 |
* put your comment there... |
| 22 |
* |
| 23 |
* @var mixed |
| 24 |
*/ |
| 25 |
protected $definition = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* put your comment there... |
| 29 |
* |
| 30 |
* @var mixed |
| 31 |
*/ |
| 32 |
protected $packageDirectory; |
| 33 |
|
| 34 |
/** |
| 35 |
* put your comment there... |
| 36 |
* |
| 37 |
* @param mixed $packageDirectory |
| 38 |
* @return CJTPackageFile |
| 39 |
*/ |
| 40 |
public function __construct($packageDirectory) { |
| 41 |
// Initialize. |
| 42 |
$this->packageDirectory = $packageDirectory; |
| 43 |
// Load XML definition. |
| 44 |
$this->loadDefinitionFile(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* put your comment there... |
| 49 |
* |
| 50 |
*/ |
| 51 |
public function document() { |
| 52 |
return $this->definition; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Fetch specific object property that might required |
| 57 |
* specific wrapper (read file, read from external resources, |
| 58 |
* decode embedded content) for reading them. |
| 59 |
* |
| 60 |
* This method now is only supporting read content from |
| 61 |
* local file laying under the package directory. |
| 62 |
* |
| 63 |
* @param SimpleXMLElement Object to fetch the property from. |
| 64 |
* @param String Property name. |
| 65 |
* @param Array TRansporter info array |
| 66 |
* @return CJTPackageFile Returning $this. |
| 67 |
*/ |
| 68 |
public function fetchProperty($object, $propertyName, $transportersInfo = array()) { |
| 69 |
// Initialize. |
| 70 |
$attributes = $object->$propertyName->attributes(); |
| 71 |
// Tarnsporter. |
| 72 |
$transporter = (string) $attributes->locate; |
| 73 |
// Do transport only if one specified! |
| 74 |
if ($transporter) { |
| 75 |
// Get value to be transported. |
| 76 |
$oldValue = (string) $object->$propertyName; |
| 77 |
// TRansporters. |
| 78 |
switch ($transporter) { |
| 79 |
// Read from file. |
| 80 |
case 'file': |
| 81 |
// If no file name (old value) is given use transporters Info parameter |
| 82 |
// for getting the file name. |
| 83 |
$fileName = $oldValue ? $oldValue : $transportersInfo[$propertyName]; |
| 84 |
$content = file_get_contents("{$this->packageDirectory}/{$fileName}"); |
| 85 |
// Save locatted file path in 'locatted' attribute. |
| 86 |
$object->$propertyName->addAttribute('locatted', $fileName); |
| 87 |
break; |
| 88 |
default: |
| 89 |
throw new Exception('Transported is not being supported!'); |
| 90 |
break; |
| 91 |
} |
| 92 |
// Replace placeholder with real content so it can be read |
| 93 |
// by the caller! |
| 94 |
$object->$propertyName = $content; |
| 95 |
} |
| 96 |
// Chaining! |
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* put your comment there... |
| 102 |
* |
| 103 |
*/ |
| 104 |
public function getDirectory() { |
| 105 |
return $this->packageDirectory; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* put your comment there... |
| 110 |
* |
| 111 |
* @param SimpleXMLElement $object |
| 112 |
* @param mixed $transportersInfo |
| 113 |
*/ |
| 114 |
public function getItem($object = null, $transportersInfo = array()) { |
| 115 |
// Initialize. |
| 116 |
$item = array(); |
| 117 |
// Defaut object is the package object. |
| 118 |
if (!$object) { |
| 119 |
$object = $this->document(); |
| 120 |
} |
| 121 |
// Package info is all the child elements except 'objects' element. |
| 122 |
$childs = $object->children(); |
| 123 |
foreach ($childs as $field => $childObject) { |
| 124 |
// Discard child elements that has childs! |
| 125 |
if (!count($childObject->children())) { |
| 126 |
// Fetch item properties that need to be transported from |
| 127 |
// other resource (e.g file). |
| 128 |
// Items that need to have transported would have |
| 129 |
// 'locate' attributes in its XML element. |
| 130 |
if (((string) $childObject->attributes()->locate)) { |
| 131 |
$this->fetchProperty($object, $field, $transportersInfo); |
| 132 |
} |
| 133 |
// Add item name to list. |
| 134 |
$item[$field] = (string) $childObject; |
| 135 |
} |
| 136 |
} |
| 137 |
// This is it! |
| 138 |
return $item; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* put your comment there... |
| 143 |
* |
| 144 |
*/ |
| 145 |
public function getPackageName() { |
| 146 |
return basename($this->packageDirectory); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* put your comment there... |
| 151 |
* |
| 152 |
*/ |
| 153 |
protected function loadDefinitionFile() { |
| 154 |
// Get full path to package definition file. |
| 155 |
$defnitionFile = "{$this->packageDirectory}/" . self::DEFINITION_FILE_NAME; |
| 156 |
// Load Definition file into SimpleXMLElement object. |
| 157 |
$this->definition = new SimpleXMLElement(file_get_contents($defnitionFile)); |
| 158 |
} |
| 159 |
|
| 160 |
} // End class. |
| 161 |
|