AdhocTask.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * $Id: AdhocTask.php 905 2010-10-05 16:28:03Z mrook $
  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. require_once 'phing/Task.php';
  22. /**
  23. * Abstract class for creating adhoc Phing components in buildfile.
  24. *
  25. * By itself this class can be used to declare a single class within your buildfile.
  26. * You can then reference this class in any task that takes custom classes (selectors,
  27. * mappers, filters, etc.)
  28. *
  29. * Subclasses exist for conveniently declaring and registering tasks and types.
  30. *
  31. * @author Hans Lellelid <hans@xmpl.org>
  32. * @version $Revision: 905 $
  33. * @package phing.tasks.system
  34. */
  35. class AdhocTask extends Task {
  36. /**
  37. * The PHP script
  38. * @var string
  39. */
  40. protected $script;
  41. protected $newClasses = array();
  42. /**
  43. * Main entry point
  44. */
  45. public function main() {
  46. $this->execute();
  47. if ($this->newClasses) {
  48. foreach($this->newClasses as $classname) {
  49. $this->log("Added adhoc class " . $classname, Project::MSG_VERBOSE);
  50. }
  51. } else {
  52. $this->log("Adhoc task executed but did not result in any new classes.", Project::MSG_VERBOSE);
  53. }
  54. }
  55. /**
  56. * Get array of names of newly defined classes.
  57. * @return array
  58. */
  59. protected function getNewClasses() {
  60. return $this->newClasses;
  61. }
  62. /**
  63. * Load the adhoc class, and perform any core validation.
  64. * @return string The classname of the ProjectComponent class.
  65. * @throws BuildException - if more than one class is defined.
  66. */
  67. protected function execute() {
  68. $classes = get_declared_classes();
  69. eval($this->script);
  70. $this->newClasses = array_diff(get_declared_classes(), $classes);
  71. }
  72. /**
  73. * Set the script.
  74. * @param string $script
  75. */
  76. public function addText($script) {
  77. $this->script = $script;
  78. }
  79. }