123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- include_once 'phing/types/Reference.php';
- include_once 'phing/types/Path.php';
- include_once 'phing/util/StringHelper.php';
- class IntrospectionHelper {
-
- private $attributeSetters = array();
-
- private $nestedCreators = array();
-
- private $nestedStorers = array();
-
-
- private $nestedTypes = array();
-
-
- private $slotListeners = array();
-
-
- private $methodAddText = null;
-
- private $bean;
-
-
- private static $helpers = array();
-
-
- public static function getHelper($class) {
- if (!isset(self::$helpers[$class])) {
- self::$helpers[$class] = new IntrospectionHelper($class);
- }
- return self::$helpers[$class];
- }
-
- function __construct($class) {
-
- $this->bean = new ReflectionClass($class);
-
-
- foreach($this->bean->getMethods() as $method) {
-
- if ($method->isPublic()) {
-
-
-
-
- $name = strtolower($method->getName());
-
-
-
-
- if ($name === "setlocation" || $name === "settasktype" || $name === "addtask") {
- continue;
- }
-
- if ($name === "addtext") {
-
- $this->methodAddText = $method;
-
- } elseif (strpos($name, "setlistening") === 0) {
-
-
-
-
-
-
-
-
-
- if (count($method->getParameters()) !== 1) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() must take exactly one parameter.");
- }
-
- $this->slotListeners[$name] = $method;
-
- } elseif (strpos($name, "set") === 0) {
-
-
-
- if (count($method->getParameters()) !== 1) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() must take exactly one parameter.");
- }
-
- $this->attributeSetters[$name] = $method;
-
- } elseif (strpos($name, "create") === 0) {
-
- if (count($method->getParameters()) > 0) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() may not take any parameters.");
- }
-
-
-
-
-
-
-
-
-
-
-
-
- preg_match('/@return[\s]+([\w]+)/', $method->getDocComment(), $matches);
- if (!empty($matches[1]) && class_exists($matches[1], false)) {
- $this->nestedTypes[$name] = $matches[1];
- } else {
-
-
- $this->nestedTypes[$name] = $this->getPropertyName($name, "create");
- }
-
- $this->nestedCreators[$name] = $method;
-
- } elseif (strpos($name, "addconfigured") === 0) {
-
-
-
-
- $params = $method->getParameters();
-
- if (count($params) < 1) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() must take at least one parameter.");
- }
-
- if (count($params) > 1) {
- $this->warn($method->getDeclaringClass()->getName()."::".$method->getName()."() takes more than one parameter. (IH only uses the first)");
- }
-
- $classname = null;
-
- if (($hint = $params[0]->getClass()) !== null) {
- $classname = $hint->getName();
- }
-
- if ($classname === null) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() method MUST use a class hint to indicate the class type of parameter.");
- }
-
- $this->nestedTypes[$name] = $classname;
-
- $this->nestedStorers[$name] = $method;
-
- } elseif (strpos($name, "add") === 0) {
-
-
-
-
- $params = $method->getParameters();
- if (count($params) < 1) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() must take at least one parameter.");
- }
-
- if (count($params) > 1) {
- $this->warn($method->getDeclaringClass()->getName()."::".$method->getName()."() takes more than one parameter. (IH only uses the first)");
- }
- $classname = null;
-
- if (($hint = $params[0]->getClass()) !== null) {
- $classname = $hint->getName();
- }
-
-
-
- if ($classname === null) {
- throw new BuildException($method->getDeclaringClass()->getName()."::".$method->getName()."() method MUST use a class hint to indicate the class type of parameter.");
- }
-
- $this->nestedCreators[$name] = $method;
- }
- }
- }
- }
-
- function setAttribute(Project $project, $element, $attributeName, &$value) {
-
-
-
-
-
-
-
-
-
-
-
- if (StringHelper::isSlotVar($value)) {
-
- $as = "setlistening" . strtolower($attributeName);
- if (!isset($this->slotListeners[$as])) {
- $msg = $this->getElementName($project, $element) . " doesn't support a slot-listening '$attributeName' attribute.";
- throw new BuildException($msg);
- }
-
- $method = $this->slotListeners[$as];
-
- $key = StringHelper::slotVar($value);
- $value = Register::getSlot($key);
-
- } else {
-
-
-
- $as = "set".strtolower($attributeName);
-
- if (!isset($this->attributeSetters[$as])) {
- $msg = $this->getElementName($project, $element) . " doesn't support the '$attributeName' attribute.";
- throw new BuildException($msg);
- }
-
- $method = $this->attributeSetters[$as];
-
- if ($as == "setrefid") {
- $value = new Reference($value);
- } else {
-
-
- if (StringHelper::isBoolean($value)) {
- $value = StringHelper::booleanValue($value);
- }
-
-
-
- $params = $method->getParameters();
- $classname = null;
-
- if (($hint = $params[0]->getClass()) !== null) {
- $classname = $hint->getName();
- }
-
-
- if ($classname !== null) {
- switch(strtolower($classname)) {
- case "phingfile":
- $value = $project->resolveFile($value);
- break;
- case "path":
- $value = new Path($project, $value);
- break;
- case "reference":
- $value = new Reference($value);
- break;
-
- }
-
- }
-
- }
-
- }
-
- try {
- $project->log(" -calling setter ".$method->getDeclaringClass()->getName()."::".$method->getName()."()", Project::MSG_DEBUG);
- $method->invoke($element, $value);
- } catch(Exception $exc) {
- throw new BuildException($exc);
- }
-
- }
-
- function addText(Project $project, $element, $text) {
- if ($this->methodAddText === null) {
- $msg = $this->getElementName($project, $element)." doesn't support nested text data.";
- throw new BuildException($msg);
- }
- try {
- $method = $this->methodAddText;
- $method->invoke($element, $text);
- } catch (Exception $exc) {
- throw new BuildException($exc);
- }
- }
-
- function createElement(Project $project, $element, $elementName) {
-
- $addMethod = "add".strtolower($elementName);
- $createMethod = "create".strtolower($elementName);
- $nestedElement = null;
-
- if (isset($this->nestedCreators[$createMethod])) {
-
- $method = $this->nestedCreators[$createMethod];
- try {
- $project->log(" -calling creator ".$method->getDeclaringClass()->getName()."::".$method->getName()."()", Project::MSG_DEBUG);
- $nestedElement = $method->invoke($element);
- } catch (Exception $exc) {
- throw new BuildException($exc);
- }
-
- } elseif (isset($this->nestedCreators[$addMethod])) {
-
- $method = $this->nestedCreators[$addMethod];
-
-
-
- try {
-
- $project->log(" -calling adder ".$method->getDeclaringClass()->getName()."::".$method->getName()."()", Project::MSG_DEBUG);
-
-
- $params = $method->getParameters();
- $classname = null;
-
- if (($hint = $params[0]->getClass()) !== null) {
- $classname = $hint->getName();
- }
-
-
- $nestedElement = new $classname();
-
- $method->invoke($element, $nestedElement);
-
- } catch (Exception $exc) {
- throw new BuildException($exc);
- }
- } else {
- $msg = $this->getElementName($project, $element) . " doesn't support the '$elementName' creator/adder.";
- throw new BuildException($msg);
- }
-
- if ($nestedElement instanceof ProjectComponent) {
- $nestedElement->setProject($project);
- }
-
- return $nestedElement;
- }
-
- function storeElement($project, $element, $child, $elementName = null) {
-
- if ($elementName === null) {
- return;
- }
-
- $storer = "addconfigured".strtolower($elementName);
-
- if (isset($this->nestedStorers[$storer])) {
-
- $method = $this->nestedStorers[$storer];
-
- try {
- $project->log(" -calling storer ".$method->getDeclaringClass()->getName()."::".$method->getName()."()", Project::MSG_DEBUG);
- $method->invoke($element, $child);
- } catch (Exception $exc) {
- throw new BuildException($exc);
- }
- }
-
- }
-
- function supportsCharacters() {
- return ($this->methodAddText !== null);
- }
-
- function getAttributes() {
- $attribs = array();
- foreach (array_keys($this->attributeSetters) as $setter) {
- $attribs[] =$this->getPropertyName($setter, "set");
- }
- return $attribs;
- }
-
- function getNestedElements() {
- return $this->nestedTypes;
- }
-
-
- function getElementName(Project $project, $element) {
-
- $taskdefs = $project->getTaskDefinitions();
- $typedefs = $project->getDataTypeDefinitions();
-
-
-
- $elClass = get_class($element);
-
- if (!in_array('getTag', get_class_methods($elClass))) {
-
-
- foreach(array_merge($taskdefs, $typedefs) as $elName => $class) {
- if (0 === strcasecmp($elClass, StringHelper::unqualify($class))) {
- return $class;
- }
- }
- return "$elClass (unknown)";
- } else {
-
- $elName = $element->getTag();
- if (isset($taskdefs[$elName])) {
- return $taskdefs[$elName];
- } elseif (isset($typedefs[$elName])) {
- return $typedefs[$elName];
- } else {
- return "$elName (unknown)";
- }
- }
- }
-
- function getPropertyName($methodName, $prefix) {
- $start = strlen($prefix);
- return strtolower(substr($methodName, $start));
- }
-
-
- function warn($msg) {
- if (Phing::getMsgOutputLevel() === Project::MSG_DEBUG) {
- print("[IntrospectionHelper] " . $msg . "\n");
- }
- }
- }
|