PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/shopaholiccompany/shopaholic
PHP | 172 lines | 118 code | 28 blank | 26 comment | 16 complexity | a8e643de667c1fcf4ccfe250042d07e4 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: Abstract.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_Abstract
  20. {
  21. protected $_basePath;
  22. protected $_props;
  23. // General
  24. public function __construct($spec)
  25. {
  26. if( is_array($spec) ) {
  27. $this->setOptions($spec);
  28. }
  29. }
  30. public function setOptions(array $options)
  31. {
  32. foreach( $options as $key => $value ) {
  33. $method = 'set' . ucfirst($key);
  34. if( method_exists($this, $method) ) {
  35. $this->$method($value);
  36. }
  37. }
  38. return $this;
  39. }
  40. public function getEntityType()
  41. {
  42. return strtolower(ltrim(strrchr(get_class($this), '_'), '_'));
  43. }
  44. public function setBasePath($path)
  45. {
  46. $this->_basePath = $path;
  47. return $this;
  48. }
  49. public function getBasePath()
  50. {
  51. if( empty($this->_basePath) ) {
  52. if( defined('APPLICATION_PATH') && is_dir(APPLICATION_PATH) ) {
  53. $this->_basePath = APPLICATION_PATH;
  54. } else {
  55. throw new Engine_Package_Manifest_Exception('base path cannot be empty');
  56. }
  57. }
  58. return $this->_basePath;
  59. }
  60. // Data
  61. public function toArray()
  62. {
  63. $array = array();
  64. if( isset($this->_props) && is_array($this->_props) ) {
  65. foreach( $this-> _props as $key ) {
  66. $method = 'get' . ucfirst($key);
  67. if( method_exists($this, $method) ) {
  68. $pval = $this->$method();
  69. $array[$key] = $pval;
  70. }
  71. }
  72. }
  73. return $array;
  74. }
  75. public function fromArray($array)
  76. {
  77. $this->setOptions($array);
  78. return $this;
  79. }
  80. public function addToArchive(Archive_Tar $archive)
  81. {
  82. // Do nothing
  83. }
  84. // Static
  85. static public function build($directory)
  86. {
  87. $list = array();
  88. $unique = array();
  89. // Build
  90. $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::KEY_AS_PATHNAME), RecursiveIteratorIterator::SELF_FIRST);
  91. while( $it->valid() ) {
  92. $key = $it->key();
  93. // Make sure it's unique, Skip .svn files
  94. if( isset($unique[$key]) || stripos($key, '.svn') !== false ) {
  95. $it->next();
  96. continue;
  97. }
  98. $unique[$key] = true;
  99. // Add
  100. $subpath = $it->getSubPathName();
  101. // Skip dot files, package files and .svn or CVS folders
  102. if( !$it->isDot() &&
  103. substr(basename($subpath), 0, strrpos(basename($subpath), '.')) != 'package' &&
  104. basename($subpath) != '.svn' &&
  105. basename($subpath) != 'CVS' ) {
  106. $key = $it->key();
  107. //$list[$it->getSubPathName()] = array(
  108. $list[] = array(
  109. 'path' => self::fix_path($it->getSubPathName()),
  110. 'dir' => $it->isDir(),
  111. 'file' => $it->isFile(),
  112. 'perms' => substr(sprintf('%o', $it->getPerms()), -4), // @todo test on windows
  113. 'size' => $it->getSize(),
  114. 'sha1' => ( $it->isFile() ? sha1_file($key) : null),
  115. );
  116. }
  117. $it->next();
  118. }
  119. ksort($list);
  120. return $list;
  121. }
  122. static public function build_file($file)
  123. {
  124. if( !file_exists($file) /* || !is_file($file) */ ) {
  125. throw new Engine_Package_Exception(sprintf('File does not exist: %s', $file));
  126. }
  127. return array(
  128. 'path' => basename($file),
  129. 'dir' => is_dir($file),
  130. 'file' => is_file($file),
  131. 'perms' => substr(sprintf('%o', fileperms($file)), -4), // @todo test on windows
  132. 'size' => filesize($file),
  133. 'sha1' => sha1_file($file),
  134. );
  135. }
  136. static public function sanitize_path($path)
  137. {
  138. return strtolower(preg_replace('/[^a-zA-Z0-9]+/', '_', $path));
  139. }
  140. static public function fix_path($path)
  141. {
  142. return str_replace(array('/', '\\'), '/', $path);
  143. }
  144. }