PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/Build/BuilderElement/Task/Filesystem/Chmod.php

http://cintient.googlecode.com/
PHP | 199 lines | 146 code | 8 blank | 45 comment | 25 complexity | d8d132d284a85da2dd2266a6f2656472 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. *
  4. * Cintient, Continuous Integration made simple.
  5. * Copyright (c) 2010, 2011, Pedro Mata-Mouros Fonseca
  6. *
  7. * This file is part of Cintient.
  8. *
  9. * Cintient is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Cintient is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with Cintient. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * Changes the permissions on a specific set of files/directories.
  25. *
  26. * @package Build
  27. * @subpackage Task
  28. * @author Pedro Mata-Mouros Fonseca <pedro.matamouros@gmail.com>
  29. * @copyright 2010-2011, Pedro Mata-Mouros Fonseca.
  30. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPLv3 or later.
  31. * @version $LastChangedRevision: 363 $
  32. * @link $HeadURL: http://cintient.googlecode.com/svn/trunk/src/core/Build/BuilderElement/Task/Filesystem/Chmod.php $
  33. * Changed by $LastChangedBy: pedro.matamouros $
  34. * Changed on $LastChangedDate: 2011-09-26 01:32:38 +0200 (Mon, 26 Sep 2011) $
  35. */
  36. class Build_BuilderElement_Task_Filesystem_Chmod extends Build_BuilderElement
  37. {
  38. protected $_file; // A string containing a single file or dir to act upon
  39. protected $_mode; // A string representation of the new permissions (octal, i.e. with a leading 0)
  40. protected $_filesets; // An array of fileset types
  41. public function __construct()
  42. {
  43. parent::__construct();
  44. $this->_file = null;
  45. $this->_mode = null;
  46. $this->_filesets = null;
  47. }
  48. /**
  49. * Creates a new instance of this builder element, with default values.
  50. */
  51. static public function create()
  52. {
  53. $o = new self();
  54. $fileset = new Build_BuilderElement_Type_Fileset();
  55. $o->setFilesets(array($fileset));
  56. return $o;
  57. }
  58. public function toAnt()
  59. {
  60. $xml = new XmlDoc();
  61. $xml->startElement('chmod');
  62. if (!$this->getFile() && !$this->getFilesets()) {
  63. SystemEvent::raise(SystemEvent::ERROR, 'No files set for task chmod.', __METHOD__);
  64. return false;
  65. }
  66. $mode = $this->getMode();
  67. if (empty($mode) || !preg_match('/^\d{3}$/', $mode)) {
  68. SystemEvent::raise(SystemEvent::ERROR, 'No mode set for task chmod.', __METHOD__);
  69. return false;
  70. }
  71. $xml->writeAttribute('perm', $mode);
  72. if ($this->getFile()) {
  73. $xml->writeAttribute('file', $this->getFile());
  74. } elseif ($this->getFilesets()) {
  75. $filesets = $this->getFilesets();
  76. foreach ($filesets as $fileset) {
  77. $xml->writeRaw($fileset->toAnt());
  78. }
  79. }
  80. $xml->endElement();
  81. return $xml->flush();
  82. }
  83. public function toHtml()
  84. {
  85. parent::toHtml();
  86. if (!$this->isVisible()) {
  87. return true;
  88. }
  89. $o = $this;
  90. h::li(array('class' => 'builderElement', 'id' => $o->getInternalId()), function() use ($o) {
  91. $o->getHtmlTitle(array('title' => 'Chmod'));
  92. h::div(array('class' => 'builderElementForm'), function() use ($o) {
  93. $o->toHtmlFailOnError();
  94. /*
  95. // File, textfield
  96. h::div(array('class' => 'label'), 'File');
  97. h::div(array('class' => 'textfieldContainer'), function() use ($o) {
  98. h::input(array('class' => 'textfield', 'type' => 'text', 'name' => 'file', 'value' => $o->getFile()));
  99. });*/
  100. // Mode, textfield
  101. h::div(array('class' => 'label'), 'Mode <span class="fineprintLabel">(e.g., 755, 644, 640, etc)</span>');
  102. h::div(array('class' => 'textfieldContainer'), function() use ($o) {
  103. h::input(array('class' => 'textfield', 'type' => 'text', 'name' => 'mode', 'value' => $o->getMode()));
  104. });
  105. // Filesets
  106. if ($o->getFilesets()) {
  107. $filesets = $o->getFilesets();
  108. foreach ($filesets as $fileset) {
  109. $fileset->toHtml();
  110. }
  111. }
  112. });
  113. });
  114. }
  115. public function toPhing()
  116. {
  117. $xml = new XmlDoc();
  118. $xml->startElement('chmod');
  119. if (!$this->getFile() && !$this->getFilesets()) {
  120. SystemEvent::raise(SystemEvent::ERROR, 'No files set for task chmod.', __METHOD__);
  121. return false;
  122. }
  123. $mode = $this->getMode();
  124. if (empty($mode) || !preg_match('/^\d{3}$/', $mode)) {
  125. SystemEvent::raise(SystemEvent::ERROR, 'No mode set for task chmod.', __METHOD__);
  126. return false;
  127. }
  128. $xml->writeAttribute('mode', $mode);
  129. if ($this->getFile()) {
  130. $xml->writeAttribute('file', $this->getFile());
  131. } elseif ($this->getFilesets()) {
  132. $filesets = $this->getFilesets();
  133. foreach ($filesets as $fileset) {
  134. $xml->writeRaw($fileset->toPhing());
  135. }
  136. }
  137. $xml->endElement();
  138. return $xml->flush();
  139. }
  140. public function toPhp(Array &$context = array())
  141. {
  142. $php = '';
  143. if (!$this->getFile() && !$this->getFilesets()) {
  144. SystemEvent::raise(SystemEvent::ERROR, 'No files not set for task chmod.', __METHOD__);
  145. return false;
  146. }
  147. $mode = $this->getMode();
  148. if (empty($mode) || !preg_match('/^(?:\d{3}|\$\{\w*\})$/', $mode)) { // It must be a 3 digit decimal or a property
  149. SystemEvent::raise(SystemEvent::ERROR, 'No mode set for chmod.', __METHOD__);
  150. return false;
  151. }
  152. $php .= "
  153. \$GLOBALS['result']['task'] = 'chmod';
  154. \$callback = function (\$entry) {
  155. \$getModeInt = expandStr('{$this->getMode()}');
  156. \$getModeOctal = intval(\$getModeInt, 8); // Casts the decimal string representation into an octal (8 is for base 8 conversion)
  157. \$ret = @chmod(\$entry, \$getModeOctal);
  158. if (!\$ret) {
  159. output(\"Failed setting \$getModeInt on \$entry.\");
  160. } else {
  161. output(\"Ok setting \$getModeInt on \$entry.\");
  162. }
  163. return \$ret;
  164. };";
  165. if ($this->getFile()) {
  166. $php .= "
  167. \$getFile = expandStr('{$this->getFile()}');
  168. if (!\$callback(\$getFile) && {$this->getFailOnError()}) { // failonerror
  169. \$GLOBALS['result']['ok'] = false;
  170. return false;
  171. } else {
  172. \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;
  173. }
  174. ";
  175. } elseif ($this->getFilesets()) { // If file exists, it takes precedence over filesets
  176. $filesets = $this->getFilesets();
  177. foreach ($filesets as $fileset) {
  178. $php .= "
  179. " . $fileset->toPhp($context) . "
  180. if (!fileset{$fileset->getId()}_{$context['id']}(\$callback) && {$this->getFailOnError()}) {
  181. \$GLOBALS['result']['ok'] = false;
  182. return false;
  183. } else {
  184. \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;
  185. }
  186. ";
  187. }
  188. }
  189. return $php;
  190. }
  191. }