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

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/phing/types/PatternSet.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 491 lines | 284 code | 49 blank | 158 comment | 47 complexity | ef6fc62a6cdb0a1730e36bd36cf03d5f MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: PatternSet.php 257 2007-10-21 00:27:07Z hans $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/system/io/FileReader.php';
  22. include_once 'phing/types/DataType.php';
  23. /**
  24. * The patternset storage component. Carries all necessary data and methods
  25. * for the patternset stuff.
  26. *
  27. * @author Andreas Aderhold, andi@binarycloud.com
  28. * @version $Revision: 1.8 $
  29. * @package phing.types
  30. */
  31. class PatternSet extends DataType {
  32. private $includeList = array();
  33. private $excludeList = array();
  34. private $includesFileList = array();
  35. private $excludesFileList = array();
  36. /**
  37. * Makes this instance in effect a reference to another PatternSet
  38. * instance.
  39. * You must not set another attribute or nest elements inside
  40. * this element if you make it a reference.
  41. */
  42. function setRefid(Reference $r) {
  43. if (!empty($this->includeList) || !empty($this->excludeList)) {
  44. throw $this->tooManyAttributes();
  45. }
  46. parent::setRefid($r);
  47. }
  48. /**
  49. * Add a name entry on the include list
  50. *
  51. * @returns PatternSetNameEntry Reference to object
  52. * @throws BuildException
  53. */
  54. function createInclude() {
  55. if ($this->isReference()) {
  56. throw $this->noChildrenAllowed();
  57. }
  58. return $this->addPatternToList($this->includeList);
  59. }
  60. /**
  61. * Add a name entry on the include files list
  62. *
  63. * @returns PatternSetNameEntry Reference to object
  64. * @throws BuildException
  65. */
  66. function createIncludesFile() {
  67. if ($this->isReference()) {
  68. throw $this->noChildrenAllowed();
  69. }
  70. return $this->addPatternToList($this->includesFileList);
  71. }
  72. /**
  73. * Add a name entry on the exclude list
  74. *
  75. * @returns PatternSetNameEntry Reference to object
  76. * @throws BuildException
  77. */
  78. function createExclude() {
  79. if ($this->isReference()) {
  80. throw $this->noChildrenAllowed();
  81. }
  82. return $this->addPatternToList($this->excludeList);
  83. }
  84. /**
  85. * add a name entry on the exclude files list
  86. *
  87. * @returns PatternSetNameEntry Reference to object
  88. * @throws BuildException
  89. */
  90. function createExcludesFile() {
  91. if ($this->isReference()) {
  92. throw $this->noChildrenAllowed();
  93. return;
  94. }
  95. return $this->addPatternToList($this->excludesFileList);
  96. }
  97. /**
  98. * Sets the set of include patterns. Patterns may be separated by a comma
  99. * or a space.
  100. *
  101. * @param string the string containing the include patterns
  102. * @returns void
  103. * @throws BuildException
  104. */
  105. function setIncludes($includes) {
  106. if ($this->isReference()) {
  107. throw $this->tooManyAttributes();
  108. }
  109. if ($includes !== null && strlen($includes) > 0) {
  110. $tok = strtok($includes, ", ");
  111. while ($tok !== false) {
  112. $o = $this->createInclude();
  113. $o->setName($tok);
  114. $tok = strtok(", ");
  115. }
  116. }
  117. }
  118. /**
  119. * Sets the set of exclude patterns. Patterns may be separated by a comma
  120. * or a space.
  121. *
  122. * @param string the string containing the exclude patterns
  123. * @returns void
  124. * @throws BuildException
  125. */
  126. function setExcludes($excludes) {
  127. if ($this->isReference()) {
  128. throw $this->tooManyAttributes();
  129. }
  130. if ($excludes !== null && strlen($excludes) > 0) {
  131. $tok = strtok($excludes, ", ");
  132. while ($tok !== false) {
  133. $o = $this->createExclude();
  134. $o->setName($tok);
  135. $tok = strtok(", ");
  136. }
  137. }
  138. }
  139. /**
  140. * add a name entry to the given list
  141. *
  142. * @param array List onto which the nameentry should be added
  143. * @returns PatternSetNameEntry Reference to the created PsetNameEntry instance
  144. */
  145. private function addPatternToList(&$list) {
  146. $num = array_push($list, new PatternSetNameEntry());
  147. return $list[$num-1];
  148. }
  149. /**
  150. * Sets the name of the file containing the includes patterns.
  151. *
  152. * @param includesFile The file to fetch the include patterns from.
  153. */
  154. function setIncludesFile($includesFile) {
  155. if ($this->isReference()) {
  156. throw $this->tooManyAttributes();
  157. }
  158. if ($includesFile instanceof File) {
  159. $includesFile = $includesFile->getPath();
  160. }
  161. $o = $this->createIncludesFile();
  162. $o->setName($includesFile);
  163. }
  164. /**
  165. * Sets the name of the file containing the excludes patterns.
  166. *
  167. * @param excludesFile The file to fetch the exclude patterns from.
  168. */
  169. function setExcludesFile($excludesFile) {
  170. if ($this->isReference()) {
  171. throw $this->tooManyAttributes();
  172. }
  173. if ($excludesFile instanceof File) {
  174. $excludesFile = $excludesFile->getPath();
  175. }
  176. $o = $this->createExcludesFile();
  177. $o->setName($excludesFile);
  178. }
  179. /**
  180. * Reads path matching patterns from a file and adds them to the
  181. * includes or excludes list
  182. */
  183. private function readPatterns(PhingFile $patternfile, &$patternlist, Project $p) {
  184. $patternReader = null;
  185. try {
  186. // Get a FileReader
  187. $patternReader = new BufferedReader(new FileReader($patternfile));
  188. // Create one NameEntry in the appropriate pattern list for each
  189. // line in the file.
  190. $line = $patternReader->readLine();
  191. while ($line !== null) {
  192. if (!empty($line)) {
  193. $line = $p->replaceProperties($line);
  194. $this->addPatternToList($patternlist)->setName($line);
  195. }
  196. $line = $patternReader->readLine();
  197. }
  198. } catch (IOException $ioe) {
  199. $msg = "An error occured while reading from pattern file: " . $patternfile->__toString();
  200. if($patternReader) $patternReader->close();
  201. throw new BuildException($msg, $ioe);
  202. }
  203. $patternReader->close();
  204. }
  205. /** Adds the patterns of the other instance to this set. */
  206. function append($other, $p) {
  207. if ($this->isReference()) {
  208. throw new BuildException("Cannot append to a reference");
  209. }
  210. $incl = $other->getIncludePatterns($p);
  211. if ($incl !== null) {
  212. foreach($incl as $incl_name) {
  213. $o = $this->createInclude();
  214. $o->setName($incl_name);
  215. }
  216. }
  217. $excl = $other->getExcludePatterns($p);
  218. if ($excl !== null) {
  219. foreach($excl as $excl_name) {
  220. $o = $this->createExclude();
  221. $o->setName($excl_name);
  222. }
  223. }
  224. }
  225. /** Returns the filtered include patterns. */
  226. function getIncludePatterns(Project $p) {
  227. if ($this->isReference()) {
  228. $o = $this->getRef($p);
  229. return $o->getIncludePatterns($p);
  230. } else {
  231. $this->readFiles($p);
  232. return $this->makeArray($this->includeList, $p);
  233. }
  234. }
  235. /** Returns the filtered exclude patterns. */
  236. function getExcludePatterns(Project $p) {
  237. if ($this->isReference()) {
  238. $o = $this->getRef($p);
  239. return $o->getExcludePatterns($p);
  240. } else {
  241. $this->readFiles($p);
  242. return $this->makeArray($this->excludeList, $p);
  243. }
  244. }
  245. /** helper for FileSet. */
  246. function hasPatterns() {
  247. return (boolean) count($this->includesFileList) > 0 || count($this->excludesFileList) > 0
  248. || count($this->includeList) > 0 || count($this->excludeList) > 0;
  249. }
  250. /**
  251. * Performs the check for circular references and returns the
  252. * referenced PatternSet.
  253. */
  254. function getRef(Project $p) {
  255. if (!$this->checked) {
  256. $stk = array();
  257. array_push($stk, $this);
  258. $this->dieOnCircularReference($stk, $p);
  259. }
  260. $o = $this->ref->getReferencedObject($p);
  261. if (!($o instanceof PatternSet)) {
  262. $msg = $this->ref->getRefId()." doesn't denote a patternset";
  263. throw new BuildException($msg);
  264. } else {
  265. return $o;
  266. }
  267. }
  268. /** Convert a array of PatternSetNameEntry elements into an array of Strings. */
  269. private function makeArray(&$list, Project $p) {
  270. if (count($list) === 0) {
  271. return null;
  272. }
  273. $tmpNames = array();
  274. foreach($list as $ne) {
  275. $pattern = (string) $ne->evalName($p);
  276. if ($pattern !== null && strlen($pattern) > 0) {
  277. array_push($tmpNames, $pattern);
  278. }
  279. }
  280. return $tmpNames;
  281. }
  282. /** Read includesfile or excludesfile if not already done so. */
  283. private function readFiles(Project $p) {
  284. if (!empty($this->includesFileList)) {
  285. foreach($this->includesFileList as $ne) {
  286. $fileName = (string) $ne->evalName($p);
  287. if ($fileName !== null) {
  288. $inclFile = $p->resolveFile($fileName);
  289. if (!$inclFile->exists()) {
  290. throw new BuildException("Includesfile ".$inclFile->getAbsolutePath()." not found.");
  291. }
  292. $this->readPatterns($inclFile, $this->includeList, $p);
  293. }
  294. }
  295. $this->includesFileList = array();
  296. }
  297. if (!empty($this->excludesFileList)) {
  298. foreach($this->excludesFileList as $ne) {
  299. $fileName = (string) $ne->evalName($p);
  300. if ($fileName !== null) {
  301. $exclFile = $p->resolveFile($fileName);
  302. if (!$exclFile->exists()) {
  303. throw new BuildException("Excludesfile ".$exclFile->getAbsolutePath()." not found.");
  304. return;
  305. }
  306. $this->readPatterns($exclFile, $this->excludeList, $p);
  307. }
  308. }
  309. $this->excludesFileList = array();
  310. }
  311. }
  312. function toString() {
  313. // We can't compile includeList into array because, toString() does
  314. // not know about project:
  315. //
  316. // $includes = $this->makeArray($this->includeList, $this->project);
  317. // $excludes = $this->makeArray($this->excludeList, $this->project);
  318. if (empty($this->includeList)) {
  319. $includes = "empty";
  320. } else {
  321. $includes = "";
  322. foreach($this->includeList as $ne) {
  323. $includes .= $ne->toString() . ",";
  324. }
  325. $includes = rtrim($includes, ",");
  326. }
  327. if (empty($this->excludeList)) {
  328. $excludes = "empty";
  329. } else {
  330. $excludes = "";
  331. foreach($this->excludeList as $ne) {
  332. $excludes .= $ne->toString() . ",";
  333. }
  334. $excludes = rtrim($excludes, ",");
  335. }
  336. return "patternSet{ includes: $includes excludes: $excludes }";
  337. }
  338. }
  339. /**
  340. * "Internal" class for holding an include/exclude pattern.
  341. */
  342. class PatternSetNameEntry {
  343. /**
  344. * The pattern.
  345. * @var string
  346. */
  347. private $name;
  348. /**
  349. * The if-condition property for this pattern to be applied.
  350. * @var string
  351. */
  352. private $ifCond;
  353. /**
  354. * The unless-condition property for this pattern to be applied.
  355. * @var string
  356. */
  357. private $unlessCond;
  358. /**
  359. * An alias for the setName() method.
  360. * @see setName()
  361. * @param string $pattern
  362. */
  363. public function setPattern($pattern) {
  364. $this->setName($pattern);
  365. }
  366. /**
  367. * Set the pattern text.
  368. * @param string $name The pattern
  369. */
  370. public function setName($name) {
  371. $this->name = (string) $name;
  372. }
  373. /**
  374. * Sets an if-condition property for this pattern to match.
  375. * @param string $cond
  376. */
  377. public function setIf($cond) {
  378. $this->ifCond = (string) $cond;
  379. }
  380. /**
  381. * Sets an unless-condition property for this pattern to match.
  382. * @param string $cond
  383. */
  384. public function setUnless($cond) {
  385. $this->unlessCond = (string) $cond;
  386. }
  387. /**
  388. * Get the pattern text.
  389. * @return string The pattern.
  390. */
  391. public function getName() {
  392. return $this->name;
  393. }
  394. /**
  395. * Evaluates the pattern.
  396. * @return string The pattern or null if it is ruled out by a condition.
  397. */
  398. public function evalName(Project $project) {
  399. return $this->valid($project) ? $this->name : null;
  400. }
  401. /**
  402. * Checks whether pattern should be applied based on whether the if and unless
  403. * properties are set in project.
  404. * @param Project $project
  405. * @return boolean
  406. */
  407. public function valid(Project $project) {
  408. if ($this->ifCond !== null && $project->getProperty($this->ifCond) === null) {
  409. return false;
  410. } else if ($this->unlessCond !== null && $project->getProperty($this->unlessCond) !== null) {
  411. return false;
  412. }
  413. return true;
  414. }
  415. /**
  416. * Gets a string representation of this pattern.
  417. * @return string
  418. */
  419. public function toString() {
  420. $buf = $this->name;
  421. if (($this->ifCond !== null) || ($this->unlessCond !== null)) {
  422. $buf .= ":";
  423. $connector = "";
  424. if ($this->ifCond !== null) {
  425. $buf .= "if->{$this->ifCond}";
  426. $connector = ";";
  427. }
  428. if ($this->unlessCond !== null) {
  429. $buf .= "$connector unless->{$this->unlessCond}";
  430. }
  431. }
  432. return $buf;
  433. }
  434. }