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

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

http://github.com/matamouros/cintient
PHP | 193 lines | 148 code | 8 blank | 37 comment | 27 complexity | 2da4ad423361c611a0338102859c4c04 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. *
  4. * Cintient, Continuous Integration made simple.
  5. * Copyright (c) 2010-2012, Pedro Mata-Mouros <pedro.matamouros@gmail.com>
  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$
  32. * @link $HeadURL$
  33. * Changed by $LastChangedBy$
  34. * Changed on $LastChangedDate$
  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. if (!$this->isActive()) {
  61. return true;
  62. }
  63. $xml = new XmlDoc();
  64. $xml->startElement('chmod');
  65. if (!$this->getFile() && !$this->getFilesets()) {
  66. SystemEvent::raise(SystemEvent::ERROR, 'No files set for task chmod.', __METHOD__);
  67. return false;
  68. }
  69. $mode = $this->getMode();
  70. if (empty($mode) || !preg_match('/^\d{3}$/', $mode)) {
  71. SystemEvent::raise(SystemEvent::ERROR, 'No mode set for task chmod.', __METHOD__);
  72. return false;
  73. }
  74. $xml->writeAttribute('perm', $mode);
  75. if ($this->getFile()) {
  76. $xml->writeAttribute('file', $this->getFile());
  77. } elseif ($this->getFilesets()) {
  78. $filesets = $this->getFilesets();
  79. foreach ($filesets as $fileset) {
  80. $xml->writeRaw($fileset->toAnt());
  81. }
  82. }
  83. $xml->endElement();
  84. return $xml->flush();
  85. }
  86. public function toHtml(Array $_ = array(), Array $__ = array())
  87. {
  88. if (!$this->isVisible()) {
  89. return true;
  90. }
  91. $callbacks = array(
  92. array('cb' => 'getHtmlFailOnError'),
  93. array(
  94. 'cb' => 'getHtmlInputText',
  95. 'name' => 'mode',
  96. 'value' => $this->getMode(),
  97. 'help' => 'e.g., 755, 644, 640, etc.'
  98. ),
  99. array('cb' => 'getFilesets'),
  100. );
  101. parent::toHtml(array('title' => 'Chmod'), $callbacks);
  102. }
  103. public function toPhing()
  104. {
  105. if (!$this->isActive()) {
  106. return true;
  107. }
  108. $xml = new XmlDoc();
  109. $xml->startElement('chmod');
  110. if (!$this->getFile() && !$this->getFilesets()) {
  111. SystemEvent::raise(SystemEvent::ERROR, 'No files set for task chmod.', __METHOD__);
  112. return false;
  113. }
  114. $mode = $this->getMode();
  115. if (empty($mode) || !preg_match('/^\d{3}$/', $mode)) {
  116. SystemEvent::raise(SystemEvent::ERROR, 'No mode set for task chmod.', __METHOD__);
  117. return false;
  118. }
  119. $xml->writeAttribute('mode', $mode);
  120. if ($this->getFile()) {
  121. $xml->writeAttribute('file', $this->getFile());
  122. } elseif ($this->getFilesets()) {
  123. $filesets = $this->getFilesets();
  124. foreach ($filesets as $fileset) {
  125. $xml->writeRaw($fileset->toPhing());
  126. }
  127. }
  128. $xml->endElement();
  129. return $xml->flush();
  130. }
  131. public function toPhp(Array &$context = array())
  132. {
  133. if (!$this->isActive()) {
  134. return '';
  135. }
  136. $php = '';
  137. if (!$this->getFile() && !$this->getFilesets()) {
  138. SystemEvent::raise(SystemEvent::ERROR, 'No files not set for task chmod.', __METHOD__);
  139. return false;
  140. }
  141. $mode = $this->getMode();
  142. if (empty($mode) || !preg_match('/^(?:\d{3}|\$\{\w*\})$/', $mode)) { // It must be a 3 digit decimal or a property
  143. SystemEvent::raise(SystemEvent::ERROR, 'No mode set for chmod.', __METHOD__);
  144. return false;
  145. }
  146. $php .= "
  147. \$GLOBALS['result']['task'] = 'chmod';
  148. \$callback = function (\$entry) {
  149. \$getModeInt = expandStr('{$this->getMode()}');
  150. \$getModeOctal = intval(\$getModeInt, 8); // Casts the decimal string representation into an octal (8 is for base 8 conversion)
  151. \$ret = @chmod(\$entry, \$getModeOctal);
  152. if (!\$ret) {
  153. output(\"Failed setting \$getModeInt on \$entry.\");
  154. } else {
  155. output(\"Ok setting \$getModeInt on \$entry.\");
  156. }
  157. return \$ret;
  158. };";
  159. if ($this->getFile()) {
  160. $php .= "
  161. \$getFile = expandStr('{$this->getFile()}');
  162. if (!\$callback(\$getFile) && {$this->getFailOnError()}) { // failonerror
  163. \$GLOBALS['result']['ok'] = false;
  164. return false;
  165. } else {
  166. \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;
  167. }
  168. ";
  169. } elseif ($this->getFilesets()) { // If file exists, it takes precedence over filesets
  170. $filesets = $this->getFilesets();
  171. foreach ($filesets as $fileset) {
  172. $php .= "
  173. " . $fileset->toPhp($context) . "
  174. if (!fileset{$fileset->getId()}_{$context['id']}(\$callback) && {$this->getFailOnError()}) {
  175. \$GLOBALS['result']['ok'] = false;
  176. return false;
  177. } else {
  178. \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;
  179. }
  180. ";
  181. }
  182. }
  183. return $php;
  184. }
  185. }