1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Connect_Model_Extension extends Varien_Object
36: {
37: 38: 39: 40: 41:
42: protected $_targets;
43:
44: 45: 46: 47: 48:
49: protected $_package;
50:
51: 52: 53: 54: 55:
56: protected function getPackage()
57: {
58: if (!$this->_package instanceof Mage_Connect_Package) {
59: $this->_package = new Mage_Connect_Package();
60: }
61: return $this->_package;
62: }
63:
64: 65: 66: 67: 68:
69: public function generatePackageXml()
70: {
71: Mage::getSingleton('connect/session')
72: ->setLocalExtensionPackageFormData($this->getData());
73:
74: $this->_setPackage()
75: ->_setRelease()
76: ->_setAuthors()
77: ->_setDependencies()
78: ->_setContents();
79: if (!$this->getPackage()->validate()) {
80: $message = $this->getPackage()->getErrors();
81: throw Mage::exception('Mage_Core', Mage::helper('connect')->__($message[0]));
82: }
83: $this->setPackageXml($this->getPackage()->getPackageXml());
84: return $this;
85: }
86:
87: 88: 89: 90: 91:
92: protected function _setPackage()
93: {
94: $this->getPackage()
95: ->setName($this->getData('name'))
96: ->setChannel($this->getData('channel'))
97: ->setLicense($this->getData('license'), $this->getData('license_uri'))
98: ->setSummary($this->getData('summary'))
99: ->setDescription($this->getData('description'));
100: return $this;
101: }
102:
103: 104: 105: 106: 107:
108: protected function _setRelease()
109: {
110: $this->getPackage()
111: ->setDate(date('Y-m-d'))
112: ->setTime(date('H:i:s'))
113: ->setVersion($this->getData('version')?$this->getData('version'):$this->getData('release_version'))
114: ->setStability($this->getData('stability'))
115: ->setNotes($this->getData('notes'));
116: return $this;
117: }
118:
119: 120: 121: 122: 123:
124: protected function _setAuthors()
125: {
126: $authors = $this->getData('authors');
127: foreach ($authors['name'] as $i => $name) {
128: $user = $authors['user'][$i];
129: $email = $authors['email'][$i];
130: $this->getPackage()->addAuthor($name, $user, $email);
131: }
132: return $this;
133: }
134:
135:
136: protected function packageFilesToArray($filesString)
137: {
138: $packageFiles = array();
139: if($filesString) {
140: $filesArray = preg_split("/[\n\r]+/", $filesString);
141: foreach($filesArray as $file) {
142: $file = trim($file, "/");
143: $res = explode(DIRECTORY_SEPARATOR, $file, 2);
144: array_map('trim', $res);
145: if(2 == count($res)) {
146: $packageFiles[] = array('target'=>$res[0], 'path'=>$res[1]);
147: }
148: }
149: }
150: return $packageFiles;
151: }
152:
153: 154: 155: 156: 157:
158: protected function _setDependencies()
159: {
160: $this->getPackage()
161: ->clearDependencies()
162: ->setDependencyPhpVersion($this->getData('depends_php_min'), $this->getData('depends_php_max'));
163:
164: foreach ($this->getData('depends') as $deptype=>$deps) {
165: foreach ($deps['name'] as $i=>$type) {
166: if (0===$i) {
167: continue;
168: }
169: $name = $deps['name'][$i];
170: $min = !empty($deps['min'][$i]) ? $deps['min'][$i] : false;
171: $max = !empty($deps['max'][$i]) ? $deps['max'][$i] : false;
172:
173: $files = !empty($deps['files'][$i]) ? $deps['files'][$i] : false;
174: $packageFiles = $this->packageFilesToArray($files);
175:
176: if ($deptype !== 'extension') {
177: $channel = !empty($deps['channel'][$i])
178: ? $deps['channel'][$i]
179: : 'connect.magentocommerce.com/core';
180: }
181: switch ($deptype) {
182: case 'package':
183: $this->getPackage()->addDependencyPackage($name, $channel, $min, $max, $packageFiles);
184: break;
185:
186: case 'extension':
187: $this->getPackage()->addDependencyExtension($name, $min, $max);
188: break;
189: }
190: }
191: }
192: return $this;
193: }
194:
195: 196: 197: 198: 199:
200: protected function _setContents()
201: {
202: $this->getPackage()->clearContents();
203: $contents = $this->getData('contents');
204: foreach ($contents['target'] as $i=>$target) {
205: if (0===$i) {
206: continue;
207: }
208: switch ($contents['type'][$i]) {
209: case 'file':
210: $this->getPackage()->addContent($contents['path'][$i], $contents['target'][$i]);
211: break;
212:
213: case 'dir':
214: $target = $contents['target'][$i];
215: $path = $contents['path'][$i];
216: $include = $contents['include'][$i];
217: $ignore = $contents['ignore'][$i];
218: $this->getPackage()->addContentDir($target, $path, $ignore, $include);
219: break;
220: }
221: }
222: return $this;
223: }
224:
225: 226: 227: 228: 229:
230: public function savePackage()
231: {
232: if ($this->getData('file_name') != '') {
233: $fileName = $this->getData('file_name');
234: $this->unsetData('file_name');
235: } else {
236: $fileName = $this->getName();
237: }
238:
239: if (!preg_match('/^[a-z0-9]+[a-z0-9\-\_\.]*([\/\\\\]{1}[a-z0-9]+[a-z0-9\-\_\.]*)*$/i', $fileName)) {
240: return false;
241: }
242:
243: if (!$this->getPackageXml()) {
244: $this->generatePackageXml();
245: }
246: if (!$this->getPackageXml()) {
247: return false;
248: }
249:
250: $path = Mage::helper('connect')->getLocalPackagesPath();
251: if (!@file_put_contents($path . 'package.xml', $this->getPackageXml())) {
252: return false;
253: }
254:
255: $this->unsPackageXml();
256: $this->unsTargets();
257: $xml = Mage::helper('core')->assocToXml($this->getData());
258: $xml = new Varien_Simplexml_Element($xml->asXML());
259:
260:
261: $parts = explode(DS, $fileName);
262: array_pop($parts);
263: $newDir = implode(DS, $parts);
264: if ((!empty($newDir)) && (!is_dir($path . $newDir))) {
265: if (!@mkdir($path . $newDir, 0777, true)) {
266: return false;
267: }
268: }
269:
270: if (!@file_put_contents($path . $fileName . '.xml', $xml->asNiceXml())) {
271: return false;
272: }
273:
274: return true;
275: }
276:
277: 278: 279: 280: 281:
282: public function createPackage()
283: {
284: $path = Mage::helper('connect')->getLocalPackagesPath();
285: if (!Mage::getConfig()->createDirIfNotExists($path)) {
286: return false;
287: }
288: if (!$this->getPackageXml()) {
289: $this->generatePackageXml();
290: }
291: $this->getPackage()->save($path);
292: return true;
293: }
294:
295: 296: 297: 298: 299:
300: public function createPackageV1x()
301: {
302: $path = Mage::helper('connect')->getLocalPackagesPathV1x();
303: if (!Mage::getConfig()->createDirIfNotExists($path)) {
304: return false;
305: }
306: if (!$this->getPackageXml()) {
307: $this->generatePackageXml();
308: }
309: $this->getPackage()->saveV1x($path);
310: return true;
311: }
312:
313: 314: 315: 316: 317:
318: public function getStabilityOptions()
319: {
320: return array(
321: 'devel' => 'Development',
322: 'alpha' => 'Alpha',
323: 'beta' => 'Beta',
324: 'stable' => 'Stable',
325: );
326: }
327:
328: 329: 330: 331: 332:
333: public function getLabelTargets()
334: {
335: if (!is_array($this->_targets)) {
336: $objectTarget = new Mage_Connect_Package_Target();
337: $this->_targets = $objectTarget->getLabelTargets();
338: }
339: return $this->_targets;
340: }
341:
342: }
343: