PageRenderTime 66ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/Package/Manifest/Entity/Package.php

https://github.com/shopaholiccompany/shopaholic
PHP | 953 lines | 773 code | 145 blank | 35 comment | 108 complexity | a428c0e3cb67407c8bf6d819323f0cbb MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Package
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Package.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_Filter
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. * @author John Boehr <j@webligo.com>
  18. */
  19. class Engine_Package_Manifest_Entity_Package extends Engine_Package_Manifest_Entity_Abstract
  20. {
  21. protected $_type;
  22. protected $_name;
  23. protected $_version;
  24. protected $_revision;
  25. protected $_path;
  26. protected $_repository;
  27. protected $_addDirectoryToArchive = false;
  28. protected $_sourcePath;
  29. // Entities
  30. protected $_callback;
  31. protected $_dependencies;
  32. protected $_meta;
  33. protected $_permissions;
  34. protected $_structure;
  35. protected $_tests;
  36. protected $_actions;
  37. protected $_props = array(
  38. 'type',
  39. 'name',
  40. 'version',
  41. 'revision',
  42. 'path',
  43. 'repository',
  44. 'meta',
  45. 'dependencies',
  46. 'permissions',
  47. 'actions',
  48. 'callback',
  49. 'tests',
  50. 'structure',
  51. );
  52. // General
  53. public function __construct($spec = null, $options = null)
  54. {
  55. if( is_array($spec) ) {
  56. $this->setOptions($spec);
  57. }
  58. if( is_array($options) ) {
  59. $this->setOptions($options);
  60. }
  61. if( is_string($spec) ) {
  62. $this->setPath($spec);
  63. }
  64. // Build if empty structure
  65. if( null === $this->_structure && !empty($this->_path) ) {
  66. $this->read($this->_path);
  67. }
  68. }
  69. public function getKey()
  70. {
  71. return sprintf('%s-%s-%s', $this->getType(), $this->getName(), $this->getVersion());
  72. }
  73. public function getGuid()
  74. {
  75. return sprintf('%s-%s', $this->getType(), $this->getName());
  76. }
  77. public function getType()
  78. {
  79. return $this->_type;
  80. }
  81. public function setType($type)
  82. {
  83. $this->_type = (string) $type;
  84. return $this;
  85. }
  86. public function getName()
  87. {
  88. return $this->_name;
  89. }
  90. public function setName($name)
  91. {
  92. $this->_name = (string) $name;
  93. return $this;
  94. }
  95. public function getVersion()
  96. {
  97. return $this->_version;
  98. }
  99. public function setVersion($version)
  100. {
  101. $this->_version = (string) $version;
  102. return $this;
  103. }
  104. public function getRevision()
  105. {
  106. return $this->_revision;
  107. }
  108. public function setRevision($revision)
  109. {
  110. if( is_numeric($revision) ) {
  111. $this->_revision = $revision;
  112. } else if( is_string($revision) &&
  113. preg_match('~\$Revision\: (\d+) \$~', $revision, $m) ) {
  114. $this->_revision = $m[1];
  115. }
  116. return $this;
  117. }
  118. public function getPath()
  119. {
  120. if( null === $this->_path ) {
  121. throw new Engine_Package_Manifest_Exception('Path cannot be empty');
  122. }
  123. return $this->_path;
  124. }
  125. public function setPath($path)
  126. {
  127. $this->_path = $path;
  128. return $this;
  129. }
  130. public function getRepository()
  131. {
  132. return $this->_repository;
  133. }
  134. public function setRespository($repository)
  135. {
  136. $this->_repository = $repository;
  137. return $this;
  138. }
  139. public function getAddDirectoryToArchive()
  140. {
  141. return (bool) $this->_addDirectoryToArchive;
  142. }
  143. public function setAddDirectoryToArchive($flag = false)
  144. {
  145. $this->_addDirectoryToArchive = (bool) $flag;
  146. return $this;
  147. }
  148. public function getSourcePath()
  149. {
  150. return $this->_sourcePath;
  151. }
  152. // Structure
  153. public function getStructure()
  154. {
  155. if( null === $this->_structure && null !== $this->_path ) {
  156. $this->read($this->_path);
  157. }
  158. return $this->_structure;
  159. }
  160. public function setStructure(array $structure)
  161. {
  162. $this->_structure = array();
  163. foreach( $structure as $key => $value ) {
  164. if( !is_array($value) ) continue;
  165. if( !isset($value['type']) ) continue;
  166. if( !is_string($value['type']) ) continue;
  167. $method = 'set' . ucfirst($value['type']);
  168. $this->$method($value);
  169. }
  170. return $this;
  171. }
  172. public function getFileStructure($assoc = false)
  173. {
  174. $files = array();
  175. foreach( $this->getStructure() as $struct ) {
  176. if( !($struct instanceof Engine_Package_Manifest_Entity_Abstract) ) {
  177. throw new Engine_Package_Manifest_Exception('Not a package entity');
  178. }
  179. if( method_exists($struct, 'getFileStructure') ) {
  180. $files = array_merge($files, $struct->getFileStructure($assoc));
  181. }
  182. }
  183. return $files;
  184. }
  185. // Actions
  186. public function addAction($action)
  187. {
  188. if( !in_array($action, (array) $this->_actions) ) {
  189. $this->_actions[] = $action;
  190. }
  191. return $this;
  192. }
  193. public function addActions(array $actions)
  194. {
  195. foreach( $actions as $action ) {
  196. $this->addAction($action);
  197. }
  198. return $this;
  199. }
  200. public function getAction($action)
  201. {
  202. if( in_array($action, (array) $this->_actions) ) {
  203. return $action;
  204. }
  205. return null;
  206. }
  207. public function getActions()
  208. {
  209. return (array) $this->_actions;
  210. }
  211. public function hasAction($action)
  212. {
  213. return in_array($action, (array) $this->_actions);
  214. }
  215. public function setAction($action)
  216. {
  217. $this->addAction($action);
  218. return $this;
  219. }
  220. public function setActions(array $actions)
  221. {
  222. $this->addActions($actions);
  223. return $this;
  224. }
  225. // Callbacks
  226. public function getCallback()
  227. {
  228. return $this->_callback;
  229. }
  230. public function setCallback($callback, $options = null)
  231. {
  232. if( !($callback instanceof Engine_Package_Manifest_Entity_Callback) ) {
  233. $callback = new Engine_Package_Manifest_Entity_Callback($callback, $options);
  234. } else if( is_array($options) ) {
  235. $callback->setOptions($options);
  236. }
  237. $this->_callback = $callback;
  238. return $this;
  239. }
  240. // Dependencies
  241. public function addDependency($dependency, $options = null)
  242. {
  243. if( !($dependency instanceof Engine_Package_Manifest_Entity_Dependency) ) {
  244. $dependency = new Engine_Package_Manifest_Entity_Dependency($dependency, $options);
  245. } else if( is_array($options) ) {
  246. $dependency->setOptions($options);
  247. }
  248. $this->_dependencies[$dependency->getGuid()] = $dependency;
  249. return $this;
  250. }
  251. public function addDependencies(array $dependencies = null)
  252. {
  253. foreach( (array) $dependencies as $key => $value ) {
  254. $dependency = null;
  255. $options = null;
  256. if( $value instanceof Engine_Package_Manifest_Entity_Dependency ) {
  257. $dependency = $value;
  258. } else if( is_string($key) ) {
  259. $dependency = $key;
  260. $options = $value;
  261. } else {
  262. $dependency = $value;
  263. }
  264. $this->addDependency($dependency, $options);
  265. }
  266. return $this;
  267. }
  268. public function getDependencies()
  269. {
  270. return $this->_dependencies;
  271. }
  272. public function getDependency($name)
  273. {
  274. if( isset($this->_dependencies[$name]) ) {
  275. return $this->_dependencies[$name];
  276. }
  277. return null;
  278. }
  279. public function setDependencies(array $dependencies = null)
  280. {
  281. $this->addDependencies($dependencies);
  282. return $this;
  283. }
  284. public function setDependency($dependency)
  285. {
  286. $this->addDependency($dependency);
  287. return $this;
  288. }
  289. // Directories
  290. public function addDirectory($directory, $options = null)
  291. {
  292. if( !($directory instanceof Engine_Package_Manifest_Entity_Directory) ) {
  293. if( !isset($options['basePath']) ) {
  294. $options['basePath'] = $this->getBasePath();
  295. }
  296. $directory = new Engine_Package_Manifest_Entity_Directory($directory, $options);
  297. } else if( is_array($options) ) {
  298. $directory->setOptions($options);
  299. }
  300. $directory->setBasePath($this->getBasePath());
  301. $name = self::fix_path($directory->getPath());
  302. $this->_structure[$name] = $directory;
  303. return $this;
  304. }
  305. public function addDirectories(array $directories = null)
  306. {
  307. foreach( (array) $directories as $key => $value ) {
  308. $directory = null;
  309. $options = null;
  310. if( $value instanceof Engine_Package_Manifest_Entity_Directory ) {
  311. $directory = $value;
  312. } else if( is_string($key) ) {
  313. $directory = $key;
  314. $options = $value;
  315. } else {
  316. $directory = $value;
  317. }
  318. $this->addDirectory($directory, $options);
  319. }
  320. return $this;
  321. }
  322. public function getDirectories()
  323. {
  324. $directories = array();
  325. foreach( $this->_structure as $key => $value ) {
  326. if( $value instanceof Engine_Package_Manifest_Entity_Directory ) {
  327. $directories[$key] = $value;
  328. }
  329. }
  330. return $directories;
  331. }
  332. public function getDirectory($directory)
  333. {
  334. foreach( $this->_structure as $key => $value ) {
  335. if( $value instanceof Engine_Package_Manifest_Entity_Directory ) {
  336. if( $value->getPath() == $directory ) {
  337. return $value;
  338. }
  339. }
  340. }
  341. return null;
  342. }
  343. public function setDirectories(array $directories = null)
  344. {
  345. $this->addDirectories($directories);
  346. return $this;
  347. }
  348. public function setDirectory($directory, $options = null)
  349. {
  350. $this->addDirectory($directory, $options);
  351. return $this;
  352. }
  353. // Files
  354. public function addFiles(array $files = null)
  355. {
  356. foreach( (array) $files as $key => $value ) {
  357. $file = null;
  358. $options = null;
  359. if( $value instanceof Engine_Package_Manifest_Entity_File ) {
  360. $file = $value;
  361. } else if( is_string($key) ) {
  362. $file = $key;
  363. $options = $value;
  364. } else {
  365. $file = $value;
  366. }
  367. $this->addFile($file, $options);
  368. }
  369. return $this;
  370. }
  371. public function addFile($file, $options = null)
  372. {
  373. if( !($file instanceof Engine_Package_Manifest_Entity_File) ) {
  374. if( !isset($options['basePath']) ) {
  375. $options['basePath'] = $this->getBasePath();
  376. }
  377. $file = new Engine_Package_Manifest_Entity_File($file, $options);
  378. } else if( is_array($options) ) {
  379. $file->setOptions($options);
  380. }
  381. $file->setBasePath($this->getBasePath());
  382. $name = self::fix_path($file->getPath());
  383. $this->_structure[$name] = $file;
  384. return $this;
  385. }
  386. public function getFiles()
  387. {
  388. $files = array();
  389. foreach( $this->_structure as $key => $value ) {
  390. if( $value instanceof Engine_Package_Manifest_Entity_File ) {
  391. $files[$key] = $value;
  392. }
  393. }
  394. return $files;
  395. }
  396. public function getFile($file)
  397. {
  398. foreach( $this->_structure as $key => $value ) {
  399. if( $value instanceof Engine_Package_Manifest_Entity_File ) {
  400. if( $value->getPath() == $file ) {
  401. return $value;
  402. }
  403. }
  404. }
  405. return null;
  406. }
  407. public function setFiles(array $files = null)
  408. {
  409. $this->addFiles($files);
  410. return $this;
  411. }
  412. public function setFile($file, $options = null)
  413. {
  414. $this->addFile($file, $options);
  415. return $this;
  416. }
  417. // Meta
  418. public function addMeta($meta, $value = null)
  419. {
  420. if( is_string($meta) ) {
  421. $meta = array(
  422. $meta => $value,
  423. );
  424. } else if( $meta instanceof Engine_Package_Manifest_Entity_Meta ) {
  425. $meta = $meta->toArray();
  426. } else if( !is_array($meta) ) {
  427. throw new Engine_Package_Manifest_Exception(sprintf('Unknown meta format: "%s"', gettype($meta)));
  428. }
  429. if( $this->_meta instanceof Engine_Package_Manifest_Entity_Meta ) {
  430. $this->_meta->setOptions($meta);
  431. } else {
  432. $this->_meta = new Engine_Package_Manifest_Entity_Meta($meta);
  433. }
  434. return $this;
  435. }
  436. public function clearMeta()
  437. {
  438. $this->_meta = new Engine_Package_Manifest_Entity_Meta();
  439. return $this;
  440. }
  441. public function getMeta()
  442. {
  443. if( null === $this->_meta ) {
  444. $this->_meta = new Engine_Package_Manifest_Entity_Meta(array());
  445. }
  446. return $this->_meta;
  447. }
  448. public function setMeta($meta, $value = null)
  449. {
  450. if( is_string($meta) ) {
  451. $this->addMeta($meta, $value);
  452. } else if( $meta instanceof Engine_Package_Manifest_Entity_Meta ) {
  453. $this->_meta = $meta;
  454. } else if( is_array($meta) ) {
  455. $this->_meta = new Engine_Package_Manifest_Entity_Meta($meta);
  456. } else {
  457. throw new Engine_Package_Manifest_Exception(sprintf('Unknown meta format: "%s"', gettype($meta)));
  458. }
  459. return $this;
  460. }
  461. // Packages
  462. public function addPackages(array $packages = null)
  463. {
  464. foreach( (array) $packages as $key => $value ) {
  465. $package = null;
  466. $options = null;
  467. if( $value instanceof Engine_Package_Manifest_Entity_Package ) {
  468. $package = $value;
  469. } else if( is_string($key) ) {
  470. $package = $key;
  471. $options = $value;
  472. } else {
  473. $package = $value;
  474. }
  475. $this->addPackage($package, $options);
  476. }
  477. return $this;
  478. }
  479. public function addPackage($package, $options = null)
  480. {
  481. if( !($package instanceof Engine_Package_Manifest_Entity_Package) ) {
  482. if( !isset($options['basePath']) ) {
  483. $options['basePath'] = $this->getBasePath();
  484. }
  485. $package = new Engine_Package_Manifest_Entity_Package($package, $options);
  486. } else if( is_array($options) ) {
  487. $package->setOptions($options);
  488. }
  489. $package->setBasePath($this->getBasePath());
  490. $name = self::fix_path($package->getPath());
  491. $this->_structure[$name] = $package;
  492. return $this;
  493. }
  494. public function getPackages()
  495. {
  496. $packages = array();
  497. foreach( $this->_structure as $key => $value ) {
  498. if( $value instanceof Engine_Package_Manifest_Entity_Package ) {
  499. $packages[$key] = $value;
  500. }
  501. }
  502. return $packages;
  503. }
  504. public function getPackage($package)
  505. {
  506. foreach( $this->_structure as $key => $value ) {
  507. if( $value instanceof Engine_Package_Manifest_Entity_Package ) {
  508. if( $value->getPath() == $package ) {
  509. return $value;
  510. }
  511. }
  512. }
  513. return null;
  514. }
  515. public function setPackages(array $packages = null)
  516. {
  517. $this->addPackages($packages);
  518. return $this;
  519. }
  520. public function setPackage($package, $options = null)
  521. {
  522. $this->addPackage($package, $options);
  523. return $this;
  524. }
  525. // Permissions
  526. public function addPermission($permission, $options = null)
  527. {
  528. if( !($permission instanceof Engine_Package_Manifest_Entity_Permission) ) {
  529. if( !isset($options['basePath']) ) {
  530. $options['basePath'] = $this->getBasePath();
  531. }
  532. $permission = new Engine_Package_Manifest_Entity_Permission($permission, $options);
  533. } else if( is_array($options) ) {
  534. $permission->setOptions($options);
  535. }
  536. $permission->setBasePath($this->getBasePath());
  537. $name = self::fix_path($permission->getPath());
  538. $this->_permissions[$name] = $permission;
  539. return $this;
  540. }
  541. public function addPermissions(array $permissions = null)
  542. {
  543. foreach( (array) $permissions as $key => $value ) {
  544. $permission = null;
  545. $options = null;
  546. if( $value instanceof Engine_Package_Manifest_Entity_Permission ) {
  547. $permission = $value;
  548. } else if( is_string($key) ) {
  549. $permission = $key;
  550. $options = $value;
  551. } else {
  552. $permission = $value;
  553. }
  554. $this->addPermission($permission, $options);
  555. }
  556. return $this;
  557. }
  558. public function getPermissions()
  559. {
  560. $permissions = array();
  561. foreach( (array) $this->_permissions as $key => $value ) {
  562. if( $value instanceof Engine_Package_Manifest_Entity_Permission ) {
  563. $permissions[$key] = $value;
  564. }
  565. }
  566. return $permissions;
  567. }
  568. public function getPermission($permission)
  569. {
  570. foreach( (array) $this->_permissions as $key => $value ) {
  571. if( $value instanceof Engine_Package_Manifest_Entity_Permission ) {
  572. if( $value->getPath() == $permission ) {
  573. return $value;
  574. }
  575. }
  576. }
  577. return null;
  578. }
  579. public function setPermissions(array $permissions = null)
  580. {
  581. $this->addPermissions($permissions);
  582. return $this;
  583. }
  584. public function setPermission($permission, $options = null)
  585. {
  586. $this->addPermission($permission, $options);
  587. return $this;
  588. }
  589. // Tests
  590. public function addTest($test)
  591. {
  592. if( !($test instanceof Engine_Package_Manifest_Entity_Test) ) {
  593. $test = new Engine_Package_Manifest_Entity_Test($test);
  594. }
  595. $this->_tests[] = $test;
  596. return $this;
  597. }
  598. public function addTests(array $tests = null)
  599. {
  600. foreach( (array) $tests as $test ) {
  601. $this->addTest($test);
  602. }
  603. return $this;
  604. }
  605. public function getTests()
  606. {
  607. return $this->_tests;
  608. }
  609. public function setTest()
  610. {
  611. $this->addTest($options);
  612. return $this;
  613. }
  614. public function setTests(array $tests = null)
  615. {
  616. $this->addTests($tests);
  617. return $this;
  618. }
  619. // Data conversion
  620. public function read($file = null)
  621. {
  622. // Detect base path if necessary
  623. if( null === $this->_basePath ) {
  624. if( !is_string($file) ) {
  625. throw new Engine_Package_Manifest_Exception(sprintf('Unknown source format: "%s"', gettype($file)));
  626. } else if( substr($file, 1, 2) != ':\\' && $file[0] != '/' ) {
  627. throw new Engine_Package_Manifest_Exception(sprintf('Path "%s" is not absolute and no base path defined', $file));
  628. } else if( is_dir($file) ) {
  629. $this->_basePath = $file;
  630. } else if( is_file($file) ) {
  631. $this->_basePath = dirname($file);
  632. } else {
  633. $this->getBasePath(); // Initalize to default
  634. }
  635. }
  636. // Make sure file is an absolute path
  637. if( $file === $this->getBasePath() ) {
  638. // We're good
  639. } else if( substr($file, 0, strlen($this->getBasePath())) === $this->getBasePath() ) {
  640. // We're good
  641. //$file = ltrim(substr($file, - (strlen($file) - strlen($this->getBasePath()))), '/');
  642. } else {
  643. $file = $this->getBasePath() . DIRECTORY_SEPARATOR . $file;
  644. }
  645. if( is_dir($file) ) {
  646. $packageFiles = glob(rtrim($file, '/\\') . DIRECTORY_SEPARATOR . 'package.*');
  647. if( !is_array($packageFiles) || count($packageFiles) != 1 ) {
  648. throw new Engine_Package_Manifest_Exception(sprintf('Found %d package files in directory %s', count($packageFiles), $file));
  649. }
  650. $file = $file . DIRECTORY_SEPARATOR . basename($packageFiles[0]);
  651. } else if( !is_file($file) ) {
  652. throw new Engine_Package_Manifest_Exception(sprintf('Unknown source file: "%s"', $file));
  653. }
  654. $parser = Engine_Package_Manifest_Parser::factory($file);
  655. $this->fromArray($parser->fromFile($file));
  656. $this->_sourcePath = $file;
  657. return $this;
  658. }
  659. public function write($file = null)
  660. {
  661. $file = $this->_getPath($file);
  662. $parser = Engine_Package_Manifest_Parser::factory($file);
  663. $parser->toFile($this->getBasePath() . DIRECTORY_SEPARATOR . $file, $this->toArray());
  664. return $this;
  665. }
  666. public function fromString($string)
  667. {
  668. $parser = Engine_Package_Manifest_Parser::factory('json');
  669. $this->fromArray($parser->fromString($string));
  670. return $this;
  671. }
  672. public function toString($file = null)
  673. {
  674. $parser = Engine_Package_Manifest_Parser::factory($file);
  675. return $parser->toString($this->toArray());
  676. }
  677. public function toArray()
  678. {
  679. $arr = parent::toArray();
  680. // Meta
  681. if( $arr['meta'] instanceof Engine_Package_Manifest_Entity_Abstract ) {
  682. $arr['meta'] = $arr['meta']->toArray();
  683. }
  684. // Callbacks
  685. if( $arr['callback'] instanceof Engine_Package_Manifest_Entity_Callback ) {
  686. $arr['callback'] = $arr['callback']->toArray();
  687. }
  688. // Dependencies
  689. foreach( (array) $arr['dependencies'] as $key => $value ) {
  690. if( $value instanceof Engine_Package_Manifest_Entity_Abstract ) {
  691. $arr['dependencies'][$key] = array_merge(array(
  692. //'type' => $value->getEntityType(),
  693. ), $value->toArray());
  694. }
  695. }
  696. // Permissions
  697. foreach( (array) $arr['permissions'] as $key => $value ) {
  698. if( $value instanceof Engine_Package_Manifest_Entity_Abstract ) {
  699. $arr['permissions'][$key] = array_merge(array(
  700. //'type' => $value->getEntityType(),
  701. ), $value->toArray());
  702. }
  703. }
  704. // Tests
  705. foreach( (array) $arr['tests'] as $key => $value ) {
  706. if( $value instanceof Engine_Package_Manifest_Entity_Test ) {
  707. $arr['tests'][$key] = array_merge(array(
  708. //'type' => $value->getEntityType(),
  709. ), $value->toArray());
  710. }
  711. }
  712. // Structure
  713. foreach( (array) $arr['structure'] as $key => $value ) {
  714. if( $value instanceof Engine_Package_Manifest_Entity_Abstract ) {
  715. $arr['structure'][$key] = array_merge(array(
  716. 'type' => $value->getEntityType(),
  717. ), $value->toArray());
  718. }
  719. }
  720. return $arr;
  721. }
  722. public function addToArchive(Archive_Tar $archive)
  723. {
  724. // Add package file
  725. $rval = $archive->addString('application' . DIRECTORY_SEPARATOR . 'packages' .
  726. DIRECTORY_SEPARATOR . $this->getKey() . '.json', $this->toString('json'));
  727. if( $archive->isError($rval) ) {
  728. throw new Engine_Package_Manifest_Exception('Error in archive: ' . $rval->getMessage());
  729. }
  730. // Add internal structure
  731. if( $this->getAddDirectoryToArchive() ) {
  732. $rval = $archive->addModify($this->getBasePath() . DIRECTORY_SEPARATOR . $this->getPath(), null, $this->getBasePath());
  733. if( $archive->isError($rval) ) {
  734. throw new Engine_Package_Manifest_Exception('Error in archive: ' . $rval->getMessage());
  735. }
  736. } else {
  737. foreach( $this->getStructure() as $key => $value ) {
  738. if( !($value instanceof Engine_Package_Manifest_Entity_Abstract) ) continue;
  739. if( method_exists($value, 'setAddDirectoryToArchive') ) {
  740. $value->setAddDirectoryToArchive($this->getAddDirectoryToArchive());
  741. }
  742. $value->addToArchive($archive);
  743. }
  744. }
  745. }
  746. // Utility
  747. protected function _getPath($file = null)
  748. {
  749. if( null === $this->_path && null === $file ) {
  750. throw new Engine_Package_Exception('no source file defined');
  751. } else if( null === $file ) {
  752. return $this->_path;
  753. } else {
  754. return $file;
  755. }
  756. }
  757. protected function _setPath($source)
  758. {
  759. if( !is_string($source) ) {
  760. throw new Engine_Package_Manifest_Exception(sprintf('Unknown source format: "%s"', gettype($source)));
  761. } else if( is_file($this->getBasePath() . DIRECTORY_SEPARATOR . $source) ) {
  762. $this->_path = $source;
  763. $this->read($this->getBasePath() . DIRECTORY_SEPARATOR . $source);
  764. } else if( is_dir($this->getBasePath() . DIRECTORY_SEPARATOR . $source) ) {
  765. $packageFiles = glob(rtrim($this->getBasePath() . DIRECTORY_SEPARATOR . $source, '/\\') . DIRECTORY_SEPARATOR . 'package.*');
  766. if( count($packageFiles) != 1 ) {
  767. throw new Engine_Package_Manifest_Exception(sprintf('Found %d package files in target directory.', count($packageFiles)));
  768. }
  769. $this->_path = $source . '/' . basename($packageFiles[0]);
  770. $this->read($packageFiles[0]);
  771. } else {
  772. //throw new Engine_Package_Manifest_Exception(sprintf('Missing source path: "%s"', $source));
  773. }
  774. }
  775. protected function _getParserClass($ext)
  776. {
  777. if( strpos($ext, '.') !== false ) {
  778. $ext = strtolower(ltrim(strrchr($ext, '.'), '.'));
  779. }
  780. $class = 'Engine_Package_Manifest_Parser_' . ucfirst($ext);
  781. if( !class_exists($class) ) {
  782. throw new Engine_Package_Manifest_Exception(sprintf('Unknown source format "%s"', $ext));
  783. }
  784. if( !is_subclass_of($class, 'Engine_Package_Manifest_Parser_Abstract') ) {
  785. throw new Engine_Package_Manifest_Exception(sprintf('Unknown source format "%s"', $ext));
  786. }
  787. return $class;
  788. }
  789. }