PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/phing/Project.php

https://github.com/rozwell/phing
PHP | 1090 lines | 557 code | 127 blank | 406 comment | 66 complexity | d62ebb7b6c8c670c7a9115adc09758a3 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: 89ab3c61983998fdc7f1782c4fe63cdc7d499f59 $
  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/PhingFile.php';
  22. include_once 'phing/util/FileUtils.php';
  23. include_once 'phing/TaskAdapter.php';
  24. include_once 'phing/util/StringHelper.php';
  25. include_once 'phing/BuildEvent.php';
  26. include_once 'phing/input/DefaultInputHandler.php';
  27. /**
  28. * The Phing project class. Represents a completely configured Phing project.
  29. * The class defines the project and all tasks/targets. It also contains
  30. * methods to start a build as well as some properties and FileSystem
  31. * abstraction.
  32. *
  33. * @author Andreas Aderhold <andi@binarycloud.com>
  34. * @author Hans Lellelid <hans@xmpl.org>
  35. * @version $Id: 89ab3c61983998fdc7f1782c4fe63cdc7d499f59 $
  36. * @package phing
  37. */
  38. class Project {
  39. // Logging level constants.
  40. const MSG_DEBUG = 4;
  41. const MSG_VERBOSE = 3;
  42. const MSG_INFO = 2;
  43. const MSG_WARN = 1;
  44. const MSG_ERR = 0;
  45. /** contains the targets */
  46. private $targets = array();
  47. /** global filterset (future use) */
  48. private $globalFilterSet = array();
  49. /** all globals filters (future use) */
  50. private $globalFilters = array();
  51. /** Project properties map (usually String to String). */
  52. private $properties = array();
  53. /**
  54. * Map of "user" properties (as created in the Ant task, for example).
  55. * Note that these key/value pairs are also always put into the
  56. * project properties, so only the project properties need to be queried.
  57. * Mapping is String to String.
  58. */
  59. private $userProperties = array();
  60. /**
  61. * Map of inherited "user" properties - that are those "user"
  62. * properties that have been created by tasks and not been set
  63. * from the command line or a GUI tool.
  64. * Mapping is String to String.
  65. */
  66. private $inheritedProperties = array();
  67. /** task definitions for this project*/
  68. private $taskdefs = array();
  69. /** type definitions for this project */
  70. private $typedefs = array();
  71. /** holds ref names and a reference to the referred object*/
  72. private $references = array();
  73. /** The InputHandler being used by this project. */
  74. private $inputHandler;
  75. /* -- properties that come in via xml attributes -- */
  76. /** basedir (PhingFile object) */
  77. private $basedir;
  78. /** the default target name */
  79. private $defaultTarget = 'all';
  80. /** project name (required) */
  81. private $name;
  82. /** project description */
  83. private $description;
  84. /** require phing version */
  85. private $phingVersion;
  86. /** a FileUtils object */
  87. private $fileUtils;
  88. /** Build listeneers */
  89. private $listeners = array();
  90. /**
  91. * Constructor, sets any default vars.
  92. */
  93. public function __construct() {
  94. $this->fileUtils = new FileUtils();
  95. $this->inputHandler = new DefaultInputHandler();
  96. }
  97. /**
  98. * Sets the input handler
  99. * @param InputHandler $handler
  100. */
  101. public function setInputHandler(InputHandler $handler) {
  102. $this->inputHandler = $handler;
  103. }
  104. /**
  105. * Retrieves the current input handler.
  106. * @return InputHandler
  107. */
  108. public function getInputHandler() {
  109. return $this->inputHandler;
  110. }
  111. /** inits the project, called from main app */
  112. public function init() {
  113. // set builtin properties
  114. $this->setSystemProperties();
  115. // load default tasks
  116. $taskdefs = Phing::getResourcePath("phing/tasks/defaults.properties");
  117. try { // try to load taskdefs
  118. $props = new Properties();
  119. $in = new PhingFile((string)$taskdefs);
  120. if ($in === null) {
  121. throw new BuildException("Can't load default task list");
  122. }
  123. $props->load($in);
  124. $enum = $props->propertyNames();
  125. foreach($enum as $key) {
  126. $value = $props->getProperty($key);
  127. $this->addTaskDefinition($key, $value);
  128. }
  129. } catch (IOException $ioe) {
  130. throw new BuildException("Can't load default task list");
  131. }
  132. // load default tasks
  133. $typedefs = Phing::getResourcePath("phing/types/defaults.properties");
  134. try { // try to load typedefs
  135. $props = new Properties();
  136. $in = new PhingFile((string)$typedefs);
  137. if ($in === null) {
  138. throw new BuildException("Can't load default datatype list");
  139. }
  140. $props->load($in);
  141. $enum = $props->propertyNames();
  142. foreach($enum as $key) {
  143. $value = $props->getProperty($key);
  144. $this->addDataTypeDefinition($key, $value);
  145. }
  146. } catch(IOException $ioe) {
  147. throw new BuildException("Can't load default datatype list");
  148. }
  149. }
  150. /** returns the global filterset (future use) */
  151. public function getGlobalFilterSet() {
  152. return $this->globalFilterSet;
  153. }
  154. // ---------------------------------------------------------
  155. // Property methods
  156. // ---------------------------------------------------------
  157. /**
  158. * Sets a property. Any existing property of the same name
  159. * is overwritten, unless it is a user property.
  160. * @param string $name The name of property to set.
  161. * Must not be <code>null</code>.
  162. * @param string $value The new value of the property.
  163. * Must not be <code>null</code>.
  164. * @return void
  165. */
  166. public function setProperty($name, $value) {
  167. // command line properties take precedence
  168. if (isset($this->userProperties[$name])) {
  169. $this->log("Override ignored for user property " . $name, Project::MSG_VERBOSE);
  170. return;
  171. }
  172. if (isset($this->properties[$name])) {
  173. $this->log("Overriding previous definition of property " . $name, Project::MSG_VERBOSE);
  174. }
  175. $this->log("Setting project property: " . $name . " -> " . $value, Project::MSG_DEBUG);
  176. $this->properties[$name] = $value;
  177. }
  178. /**
  179. * Sets a property if no value currently exists. If the property
  180. * exists already, a message is logged and the method returns with
  181. * no other effect.
  182. *
  183. * @param string $name The name of property to set.
  184. * Must not be <code>null</code>.
  185. * @param string $value The new value of the property.
  186. * Must not be <code>null</code>.
  187. * @since 2.0
  188. */
  189. public function setNewProperty($name, $value) {
  190. if (isset($this->properties[$name])) {
  191. $this->log("Override ignored for property " . $name, Project::MSG_DEBUG);
  192. return;
  193. }
  194. $this->log("Setting project property: " . $name . " -> " . $value, Project::MSG_DEBUG);
  195. $this->properties[$name] = $value;
  196. }
  197. /**
  198. * Sets a user property, which cannot be overwritten by
  199. * set/unset property calls. Any previous value is overwritten.
  200. * @param string $name The name of property to set.
  201. * Must not be <code>null</code>.
  202. * @param string $value The new value of the property.
  203. * Must not be <code>null</code>.
  204. * @see #setProperty()
  205. */
  206. public function setUserProperty($name, $value) {
  207. $this->log("Setting ro project property: " . $name . " -> " . $value, Project::MSG_DEBUG);
  208. $this->userProperties[$name] = $value;
  209. $this->properties[$name] = $value;
  210. }
  211. /**
  212. * Sets a user property, which cannot be overwritten by set/unset
  213. * property calls. Any previous value is overwritten. Also marks
  214. * these properties as properties that have not come from the
  215. * command line.
  216. *
  217. * @param string $name The name of property to set.
  218. * Must not be <code>null</code>.
  219. * @param string $value The new value of the property.
  220. * Must not be <code>null</code>.
  221. * @see #setProperty()
  222. */
  223. public function setInheritedProperty($name, $value) {
  224. $this->inheritedProperties[$name] = $value;
  225. $this->setUserProperty($name, $value);
  226. }
  227. /**
  228. * Sets a property unless it is already defined as a user property
  229. * (in which case the method returns silently).
  230. *
  231. * @param name The name of the property.
  232. * Must not be <code>null</code>.
  233. * @param value The property value. Must not be <code>null</code>.
  234. */
  235. private function setPropertyInternal($name, $value) {
  236. if (isset($this->userProperties[$name])) {
  237. $this->log("Override ignored for user property " . $name, Project::MSG_VERBOSE);
  238. return;
  239. }
  240. $this->properties[$name] = $value;
  241. }
  242. /**
  243. * Returns the value of a property, if it is set.
  244. *
  245. * @param string $name The name of the property.
  246. * May be <code>null</code>, in which case
  247. * the return value is also <code>null</code>.
  248. * @return string The property value, or <code>null</code> for no match
  249. * or if a <code>null</code> name is provided.
  250. */
  251. public function getProperty($name) {
  252. if (!isset($this->properties[$name])) {
  253. return null;
  254. }
  255. $found = $this->properties[$name];
  256. // check to see if there are unresolved property references
  257. if (false !== strpos($found, '${')) {
  258. // attempt to resolve properties
  259. $found = $this->replaceProperties($found);
  260. // save resolved value
  261. $this->properties[$name] = $found;
  262. }
  263. return $found;
  264. }
  265. /**
  266. * Replaces ${} style constructions in the given value with the
  267. * string value of the corresponding data types.
  268. *
  269. * @param value The string to be scanned for property references.
  270. * May be <code>null</code>.
  271. *
  272. * @return the given string with embedded property names replaced
  273. * by values, or <code>null</code> if the given string is
  274. * <code>null</code>.
  275. *
  276. * @exception BuildException if the given value has an unclosed
  277. * property name, e.g. <code>${xxx</code>
  278. */
  279. public function replaceProperties($value) {
  280. return ProjectConfigurator::replaceProperties($this, $value, $this->properties);
  281. }
  282. /**
  283. * Returns the value of a user property, if it is set.
  284. *
  285. * @param string $name The name of the property.
  286. * May be <code>null</code>, in which case
  287. * the return value is also <code>null</code>.
  288. * @return string The property value, or <code>null</code> for no match
  289. * or if a <code>null</code> name is provided.
  290. */
  291. public function getUserProperty($name) {
  292. if (!isset($this->userProperties[$name])) {
  293. return null;
  294. }
  295. return $this->userProperties[$name];
  296. }
  297. /**
  298. * Returns a copy of the properties table.
  299. * @return array A hashtable containing all properties
  300. * (including user properties).
  301. */
  302. public function getProperties() {
  303. return $this->properties;
  304. }
  305. /**
  306. * Returns a copy of the user property hashtable
  307. * @return a hashtable containing just the user properties
  308. */
  309. public function getUserProperties() {
  310. return $this->userProperties;
  311. }
  312. /**
  313. * Copies all user properties that have been set on the command
  314. * line or a GUI tool from this instance to the Project instance
  315. * given as the argument.
  316. *
  317. * <p>To copy all "user" properties, you will also have to call
  318. * {@link #copyInheritedProperties copyInheritedProperties}.</p>
  319. *
  320. * @param Project $other the project to copy the properties to. Must not be null.
  321. * @return void
  322. * @since phing 2.0
  323. */
  324. public function copyUserProperties(Project $other) {
  325. foreach($this->userProperties as $arg => $value) {
  326. if (isset($this->inheritedProperties[$arg])) {
  327. continue;
  328. }
  329. $other->setUserProperty($arg, $value);
  330. }
  331. }
  332. /**
  333. * Copies all user properties that have not been set on the
  334. * command line or a GUI tool from this instance to the Project
  335. * instance given as the argument.
  336. *
  337. * <p>To copy all "user" properties, you will also have to call
  338. * {@link #copyUserProperties copyUserProperties}.</p>
  339. *
  340. * @param Project $other the project to copy the properties to. Must not be null.
  341. *
  342. * @since phing 2.0
  343. */
  344. public function copyInheritedProperties(Project $other) {
  345. foreach($this->userProperties as $arg => $value) {
  346. if ($other->getUserProperty($arg) !== null) {
  347. continue;
  348. }
  349. $other->setInheritedProperty($arg, $value);
  350. }
  351. }
  352. // ---------------------------------------------------------
  353. // END Properties methods
  354. // ---------------------------------------------------------
  355. /**
  356. * Sets default target
  357. * @param string $targetName
  358. */
  359. public function setDefaultTarget($targetName) {
  360. $this->defaultTarget = (string) trim($targetName);
  361. }
  362. /**
  363. * Returns default target
  364. * @return string
  365. */
  366. public function getDefaultTarget() {
  367. return (string) $this->defaultTarget;
  368. }
  369. /**
  370. * Sets the name of the current project
  371. *
  372. * @param string $name name of project
  373. * @return void
  374. * @access public
  375. * @author Andreas Aderhold, andi@binarycloud.com
  376. */
  377. public function setName($name) {
  378. $this->name = (string) trim($name);
  379. $this->setProperty("phing.project.name", $this->name);
  380. }
  381. /**
  382. * Returns the name of this project
  383. *
  384. * @return string projectname
  385. * @access public
  386. * @author Andreas Aderhold, andi@binarycloud.com
  387. */
  388. public function getName() {
  389. return (string) $this->name;
  390. }
  391. /**
  392. * Set the projects description
  393. * @param string $description
  394. */
  395. public function setDescription($description) {
  396. $this->description = (string) trim($description);
  397. }
  398. /**
  399. * return the description, null otherwise
  400. * @return string|null
  401. */
  402. public function getDescription() {
  403. return $this->description;
  404. }
  405. /**
  406. * Set the minimum required phing version
  407. * @param string $version
  408. */
  409. public function setPhingVersion($version) {
  410. $version = str_replace('phing', '', strtolower($version));
  411. $this->phingVersion = (string)trim($version);
  412. }
  413. /**
  414. * Get the minimum required phing version
  415. * @return string
  416. */
  417. public function getPhingVersion() {
  418. if($this->phingVersion === null) {
  419. $this->setPhingVersion(Phing::getPhingVersion());
  420. }
  421. return $this->phingVersion;
  422. }
  423. /**
  424. * Set basedir object from xm
  425. * @param PhingFile|string $dir
  426. */
  427. public function setBasedir($dir) {
  428. if ($dir instanceof PhingFile) {
  429. $dir = $dir->getAbsolutePath();
  430. }
  431. $dir = $this->fileUtils->normalize($dir);
  432. $dir = new PhingFile((string) $dir);
  433. if (!$dir->exists()) {
  434. throw new BuildException("Basedir ".$dir->getAbsolutePath()." does not exist");
  435. }
  436. if (!$dir->isDirectory()) {
  437. throw new BuildException("Basedir ".$dir->getAbsolutePath()." is not a directory");
  438. }
  439. $this->basedir = $dir;
  440. $this->setPropertyInternal("project.basedir", $this->basedir->getAbsolutePath());
  441. $this->log("Project base dir set to: " . $this->basedir->getPath(), Project::MSG_VERBOSE);
  442. // [HL] added this so that ./ files resolve correctly. This may be a mistake ... or may be in wrong place.
  443. chdir($dir->getAbsolutePath());
  444. }
  445. /**
  446. * Returns the basedir of this project
  447. *
  448. * @return PhingFile Basedir PhingFile object
  449. * @access public
  450. * @throws BuildException
  451. * @author Andreas Aderhold, andi@binarycloud.com
  452. */
  453. public function getBasedir() {
  454. if ($this->basedir === null) {
  455. try { // try to set it
  456. $this->setBasedir(".");
  457. } catch (BuildException $exc) {
  458. throw new BuildException("Can not set default basedir. ".$exc->getMessage());
  459. }
  460. }
  461. return $this->basedir;
  462. }
  463. /**
  464. * Sets system properties and the environment variables for this project.
  465. *
  466. * @return void
  467. */
  468. public function setSystemProperties() {
  469. // first get system properties
  470. $systemP = array_merge( self::getProperties(), Phing::getProperties() );
  471. foreach($systemP as $name => $value) {
  472. $this->setPropertyInternal($name, $value);
  473. }
  474. // and now the env vars
  475. foreach($_SERVER as $name => $value) {
  476. // skip arrays
  477. if (is_array($value)) {
  478. continue;
  479. }
  480. $this->setPropertyInternal('env.' . $name, $value);
  481. }
  482. return true;
  483. }
  484. /**
  485. * Adds a task definition.
  486. * @param string $name Name of tag.
  487. * @param string $class The class path to use.
  488. * @param string $classpath The classpat to use.
  489. */
  490. public function addTaskDefinition($name, $class, $classpath = null) {
  491. $name = $name;
  492. $class = $class;
  493. if ($class === "") {
  494. $this->log("Task $name has no class defined.", Project::MSG_ERR);
  495. } elseif (!isset($this->taskdefs[$name])) {
  496. Phing::import($class, $classpath);
  497. $this->taskdefs[$name] = $class;
  498. $this->log(" +Task definiton: $name ($class)", Project::MSG_DEBUG);
  499. } else {
  500. $this->log("Task $name ($class) already registerd, skipping", Project::MSG_VERBOSE);
  501. }
  502. }
  503. /**
  504. * Returns the task definitions
  505. * @return array
  506. */
  507. public function getTaskDefinitions() {
  508. return $this->taskdefs;
  509. }
  510. /**
  511. * Adds a data type definition.
  512. * @param string $typeName Name of the type.
  513. * @param string $typeClass The class to use.
  514. * @param string $classpath The classpath to use.
  515. */
  516. public function addDataTypeDefinition($typeName, $typeClass, $classpath = null) {
  517. if (!isset($this->typedefs[$typeName])) {
  518. Phing::import($typeClass, $classpath);
  519. $this->typedefs[$typeName] = $typeClass;
  520. $this->log(" +User datatype: $typeName ($typeClass)", Project::MSG_DEBUG);
  521. } else {
  522. $this->log("Type $typeName ($typeClass) already registerd, skipping", Project::MSG_VERBOSE);
  523. }
  524. }
  525. /**
  526. * Returns the data type definitions
  527. * @return array
  528. */
  529. public function getDataTypeDefinitions() {
  530. return $this->typedefs;
  531. }
  532. /**
  533. * Add a new target to the project
  534. * @param string $targetName
  535. * @param Target $target
  536. */
  537. public function addTarget($targetName, &$target) {
  538. if (isset($this->targets[$targetName])) {
  539. throw new BuildException("Duplicate target: $targetName");
  540. }
  541. $this->addOrReplaceTarget($targetName, $target);
  542. }
  543. /**
  544. * Adds or replaces a target in the project
  545. * @param string $targetName
  546. * @param Target $target
  547. */
  548. public function addOrReplaceTarget($targetName, &$target) {
  549. $this->log(" +Target: $targetName", Project::MSG_DEBUG);
  550. $target->setProject($this);
  551. $this->targets[$targetName] = $target;
  552. $ctx = $this->getReference("phing.parsing.context");
  553. $current = $ctx->getConfigurator()->getCurrentTargets();
  554. $current[$targetName] = $target;
  555. }
  556. /**
  557. * Returns the available targets
  558. * @return array
  559. */
  560. public function getTargets() {
  561. return $this->targets;
  562. }
  563. /**
  564. * Create a new task instance and return reference to it. This method is
  565. * sorta factory like. A _local_ instance is created and a reference returned to
  566. * that instance. Usually PHP destroys local variables when the function call
  567. * ends. But not if you return a reference to that variable.
  568. * This is kinda error prone, because if no reference exists to the variable
  569. * it is destroyed just like leaving the local scope with primitive vars. There's no
  570. * central place where the instance is stored as in other OOP like languages.
  571. *
  572. * [HL] Well, ZE2 is here now, and this is still working. We'll leave this alone
  573. * unless there's any good reason not to.
  574. *
  575. * @param string $taskType Task name
  576. * @return Task A task object
  577. * @throws BuildException
  578. * Exception
  579. */
  580. public function createTask($taskType) {
  581. try {
  582. $classname = "";
  583. $tasklwr = strtolower($taskType);
  584. foreach ($this->taskdefs as $name => $class) {
  585. if (strtolower($name) === $tasklwr) {
  586. $classname = $class;
  587. break;
  588. }
  589. }
  590. if ($classname === "") {
  591. return null;
  592. }
  593. $cls = Phing::import($classname);
  594. if (!class_exists($cls)) {
  595. throw new BuildException("Could not instantiate class $cls, even though a class was specified. (Make sure that the specified class file contains a class with the correct name.)");
  596. }
  597. $o = new $cls();
  598. if ($o instanceof Task) {
  599. $task = $o;
  600. } else {
  601. $this->log (" (Using TaskAdapter for: $taskType)", Project::MSG_DEBUG);
  602. // not a real task, try adapter
  603. $taskA = new TaskAdapter();
  604. $taskA->setProxy($o);
  605. $task = $taskA;
  606. }
  607. $task->setProject($this);
  608. $task->setTaskType($taskType);
  609. // set default value, can be changed by the user
  610. $task->setTaskName($taskType);
  611. $this->log (" +Task: " . $taskType, Project::MSG_DEBUG);
  612. } catch (Exception $t) {
  613. throw new BuildException("Could not create task of type: " . $taskType, $t);
  614. }
  615. // everything fine return reference
  616. return $task;
  617. }
  618. /**
  619. * Creates a new condition and returns the reference to it
  620. *
  621. * @param string $conditionType
  622. * @return Condition
  623. * @throws BuildException
  624. */
  625. public function createCondition($conditionType)
  626. {
  627. try {
  628. $classname = "";
  629. $tasklwr = strtolower($conditionType);
  630. foreach ($this->typedefs as $name => $class) {
  631. if (strtolower($name) === $tasklwr) {
  632. $classname = $class;
  633. break;
  634. }
  635. }
  636. if ($classname === "") {
  637. return null;
  638. }
  639. $cls = Phing::import($classname);
  640. if (!class_exists($cls)) {
  641. throw new BuildException("Could not instantiate class $cls, even though a class was specified. (Make sure that the specified class file contains a class with the correct name.)");
  642. }
  643. $o = new $cls();
  644. if ($o instanceof Condition) {
  645. return $o;
  646. } else {
  647. throw new BuildException("Not actually a condition");
  648. }
  649. } catch (Exception $e) {
  650. throw new BuildException("Could not create condition of type: " . $conditionType, $e);
  651. }
  652. }
  653. /**
  654. * Create a datatype instance and return reference to it
  655. * See createTask() for explanation how this works
  656. *
  657. * @param string $typeName Type name
  658. * @return object A datatype object
  659. * @throws BuildException
  660. * Exception
  661. */
  662. public function createDataType($typeName) {
  663. try {
  664. $cls = "";
  665. $typelwr = strtolower($typeName);
  666. foreach ($this->typedefs as $name => $class) {
  667. if (strtolower($name) === $typelwr) {
  668. $cls = StringHelper::unqualify($class);
  669. break;
  670. }
  671. }
  672. if ($cls === "") {
  673. return null;
  674. }
  675. if (!class_exists($cls)) {
  676. throw new BuildException("Could not instantiate class $cls, even though a class was specified. (Make sure that the specified class file contains a class with the correct name.)");
  677. }
  678. $type = new $cls();
  679. $this->log(" +Type: $typeName", Project::MSG_DEBUG);
  680. if (!($type instanceof DataType)) {
  681. throw new Exception("$class is not an instance of phing.types.DataType");
  682. }
  683. if ($type instanceof ProjectComponent) {
  684. $type->setProject($this);
  685. }
  686. } catch (Exception $t) {
  687. throw new BuildException("Could not create type: $typeName", $t);
  688. }
  689. // everything fine return reference
  690. return $type;
  691. }
  692. /**
  693. * Executes a list of targets
  694. *
  695. * @param array $targetNames List of target names to execute
  696. * @return void
  697. * @throws BuildException
  698. */
  699. public function executeTargets($targetNames) {
  700. foreach($targetNames as $tname) {
  701. $this->executeTarget($tname);
  702. }
  703. }
  704. /**
  705. * Executes a target
  706. *
  707. * @param string $targetName Name of Target to execute
  708. * @return void
  709. * @throws BuildException
  710. */
  711. public function executeTarget($targetName) {
  712. // complain about executing void
  713. if ($targetName === null) {
  714. throw new BuildException("No target specified");
  715. }
  716. // invoke topological sort of the target tree and run all targets
  717. // until targetName occurs.
  718. $sortedTargets = $this->_topoSort($targetName, $this->targets);
  719. $curIndex = (int) 0;
  720. $curTarget = null;
  721. do {
  722. try {
  723. $curTarget = $sortedTargets[$curIndex++];
  724. $curTarget->performTasks();
  725. } catch (BuildException $exc) {
  726. $this->log("Execution of target \"".$curTarget->getName()."\" failed for the following reason: ".$exc->getMessage(), Project::MSG_ERR);
  727. throw $exc;
  728. }
  729. } while ($curTarget->getName() !== $targetName);
  730. }
  731. /**
  732. * Helper function
  733. */
  734. public function resolveFile($fileName, $rootDir = null) {
  735. if ($rootDir === null) {
  736. return $this->fileUtils->resolveFile($this->basedir, $fileName);
  737. } else {
  738. return $this->fileUtils->resolveFile($rootDir, $fileName);
  739. }
  740. }
  741. /**
  742. * Topologically sort a set of Targets.
  743. * @param string $root is the (String) name of the root Target. The sort is
  744. * created in such a way that the sequence of Targets until the root
  745. * target is the minimum possible such sequence.
  746. * @param array $targets is a array representing a "name to Target" mapping
  747. * @return An array of Strings with the names of the targets in
  748. * sorted order.
  749. */
  750. public function _topoSort($root, &$targets) {
  751. $root = (string) $root;
  752. $ret = array();
  753. $state = array();
  754. $visiting = array();
  755. // We first run a DFS based sort using the root as the starting node.
  756. // This creates the minimum sequence of Targets to the root node.
  757. // We then do a sort on any remaining unVISITED targets.
  758. // This is unnecessary for doing our build, but it catches
  759. // circular dependencies or missing Targets on the entire
  760. // dependency tree, not just on the Targets that depend on the
  761. // build Target.
  762. $this->_tsort($root, $targets, $state, $visiting, $ret);
  763. $retHuman = "";
  764. for ($i=0, $_i=count($ret); $i < $_i; $i++) {
  765. $retHuman .= $ret[$i]->toString()." ";
  766. }
  767. $this->log("Build sequence for target '$root' is: $retHuman", Project::MSG_VERBOSE);
  768. $keys = array_keys($targets);
  769. while($keys) {
  770. $curTargetName = (string) array_shift($keys);
  771. if (!isset($state[$curTargetName])) {
  772. $st = null;
  773. } else {
  774. $st = (string) $state[$curTargetName];
  775. }
  776. if ($st === null) {
  777. $this->_tsort($curTargetName, $targets, $state, $visiting, $ret);
  778. } elseif ($st === "VISITING") {
  779. throw new Exception("Unexpected node in visiting state: $curTargetName");
  780. }
  781. }
  782. $retHuman = "";
  783. for ($i=0,$_i=count($ret); $i < $_i; $i++) {
  784. $retHuman .= $ret[$i]->toString()." ";
  785. }
  786. $this->log("Complete build sequence is: $retHuman", Project::MSG_VERBOSE);
  787. return $ret;
  788. }
  789. // one step in a recursive DFS traversal of the target dependency tree.
  790. // - The array "state" contains the state (VISITED or VISITING or null)
  791. // of all the target names.
  792. // - The stack "visiting" contains a stack of target names that are
  793. // currently on the DFS stack. (NB: the target names in "visiting" are
  794. // exactly the target names in "state" that are in the VISITING state.)
  795. // 1. Set the current target to the VISITING state, and push it onto
  796. // the "visiting" stack.
  797. // 2. Throw a BuildException if any child of the current node is
  798. // in the VISITING state (implies there is a cycle.) It uses the
  799. // "visiting" Stack to construct the cycle.
  800. // 3. If any children have not been VISITED, tsort() the child.
  801. // 4. Add the current target to the Vector "ret" after the children
  802. // have been visited. Move the current target to the VISITED state.
  803. // "ret" now contains the sorted sequence of Targets upto the current
  804. // Target.
  805. public function _tsort($root, &$targets, &$state, &$visiting, &$ret) {
  806. $state[$root] = "VISITING";
  807. $visiting[] = $root;
  808. if (!isset($targets[$root]) || !($targets[$root] instanceof Target)) {
  809. $target = null;
  810. } else {
  811. $target = $targets[$root];
  812. }
  813. // make sure we exist
  814. if ($target === null) {
  815. $sb = "Target '$root' does not exist in this project.";
  816. array_pop($visiting);
  817. if (!empty($visiting)) {
  818. $parent = (string) $visiting[count($visiting)-1];
  819. $sb .= " It is a dependency of target '$parent'.";
  820. }
  821. throw new BuildException($sb);
  822. }
  823. $deps = $target->getDependencies();
  824. while($deps) {
  825. $cur = (string) array_shift($deps);
  826. if (!isset($state[$cur])) {
  827. $m = null;
  828. } else {
  829. $m = (string) $state[$cur];
  830. }
  831. if ($m === null) {
  832. // not been visited
  833. $this->_tsort($cur, $targets, $state, $visiting, $ret);
  834. } elseif ($m == "VISITING") {
  835. // currently visiting this node, so have a cycle
  836. throw $this->_makeCircularException($cur, $visiting);
  837. }
  838. }
  839. $p = (string) array_pop($visiting);
  840. if ($root !== $p) {
  841. throw new Exception("Unexpected internal error: expected to pop $root but got $p");
  842. }
  843. $state[$root] = "VISITED";
  844. $ret[] = $target;
  845. }
  846. public function _makeCircularException($end, $stk) {
  847. $sb = "Circular dependency: $end";
  848. do {
  849. $c = (string) array_pop($stk);
  850. $sb .= " <- ".$c;
  851. } while($c != $end);
  852. return new BuildException($sb);
  853. }
  854. /**
  855. * Adds a reference to an object. This method is called when the parser
  856. * detects a id="foo" attribute. It passes the id as $name and a reference
  857. * to the object assigned to this id as $value
  858. * @param string $name
  859. * @param object $object
  860. */
  861. public function addReference($name, $object) {
  862. if (isset($this->references[$name])) {
  863. $this->log("Overriding previous definition of reference to $name", Project::MSG_WARN);
  864. }
  865. $this->log("Adding reference: $name -> ".get_class($object), Project::MSG_DEBUG);
  866. $this->references[$name] = $object;
  867. }
  868. /**
  869. * Returns the references array.
  870. * @return array
  871. */
  872. public function getReferences() {
  873. return $this->references;
  874. }
  875. /**
  876. * Returns a specific reference.
  877. * @param string $key The reference id/key.
  878. * @return object Reference or null if not defined
  879. */
  880. public function getReference($key)
  881. {
  882. if (isset($this->references[$key])) {
  883. return $this->references[$key];
  884. }
  885. return null; // just to be explicit
  886. }
  887. /**
  888. * Abstracting and simplifyling Logger calls for project messages
  889. * @param string $msg
  890. * @param int $level
  891. */
  892. public function log($msg, $level = Project::MSG_INFO) {
  893. $this->logObject($this, $msg, $level);
  894. }
  895. function logObject($obj, $msg, $level) {
  896. $this->fireMessageLogged($obj, $msg, $level);
  897. }
  898. function addBuildListener(BuildListener $listener) {
  899. $this->listeners[] = $listener;
  900. }
  901. function removeBuildListener(BuildListener $listener) {
  902. $newarray = array();
  903. for ($i=0, $size=count($this->listeners); $i < $size; $i++) {
  904. if ($this->listeners[$i] !== $listener) {
  905. $newarray[] = $this->listeners[$i];
  906. }
  907. }
  908. $this->listeners = $newarray;
  909. }
  910. function getBuildListeners() {
  911. return $this->listeners;
  912. }
  913. function fireBuildStarted() {
  914. $event = new BuildEvent($this);
  915. foreach($this->listeners as $listener) {
  916. $listener->buildStarted($event);
  917. }
  918. }
  919. function fireBuildFinished($exception) {
  920. $event = new BuildEvent($this);
  921. $event->setException($exception);
  922. foreach($this->listeners as $listener) {
  923. $listener->buildFinished($event);
  924. }
  925. }
  926. function fireTargetStarted($target) {
  927. $event = new BuildEvent($target);
  928. foreach($this->listeners as $listener) {
  929. $listener->targetStarted($event);
  930. }
  931. }
  932. function fireTargetFinished($target, $exception) {
  933. $event = new BuildEvent($target);
  934. $event->setException($exception);
  935. foreach($this->listeners as $listener) {
  936. $listener->targetFinished($event);
  937. }
  938. }
  939. function fireTaskStarted($task) {
  940. $event = new BuildEvent($task);
  941. foreach($this->listeners as $listener) {
  942. $listener->taskStarted($event);
  943. }
  944. }
  945. function fireTaskFinished($task, $exception) {
  946. $event = new BuildEvent($task);
  947. $event->setException($exception);
  948. foreach($this->listeners as $listener) {
  949. $listener->taskFinished($event);
  950. }
  951. }
  952. function fireMessageLoggedEvent($event, $message, $priority) {
  953. $event->setMessage($message, $priority);
  954. foreach($this->listeners as $listener) {
  955. $listener->messageLogged($event);
  956. }
  957. }
  958. function fireMessageLogged($object, $message, $priority) {
  959. $this->fireMessageLoggedEvent(new BuildEvent($object), $message, $priority);
  960. }
  961. }