X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fconsolidation%2Frobo%2Fsrc%2FTask%2FDevelopment%2FGenerateTask.php;fp=vendor%2Fconsolidation%2Frobo%2Fsrc%2FTask%2FDevelopment%2FGenerateTask.php;h=9d7a698e9ca159b5f6e4ad7ef86b2d517eb0739a;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=0000000000000000000000000000000000000000;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/vendor/consolidation/robo/src/Task/Development/GenerateTask.php b/vendor/consolidation/robo/src/Task/Development/GenerateTask.php new file mode 100644 index 000000000..9d7a698e9 --- /dev/null +++ b/vendor/consolidation/robo/src/Task/Development/GenerateTask.php @@ -0,0 +1,107 @@ +taskGenerateTask('Symfony\Component\Filesystem\Filesystem', 'FilesystemStack') + * ->run(); + * ``` + */ +class GenerateTask extends BaseTask +{ + /** + * @var string + */ + protected $className; + + /** + * @var string + */ + protected $wrapperClassName; + + /** + * @param string $className + * @param string $wrapperClassName + */ + public function __construct($className, $wrapperClassName = '') + { + $this->className = $className; + $this->wrapperClassName = $wrapperClassName; + } + + /** + * {@inheritdoc} + */ + public function run() + { + $delegate = new \ReflectionClass($this->className); + $replacements = []; + + $leadingCommentChars = " * "; + $methodDescriptions = []; + $methodImplementations = []; + $immediateMethods = []; + foreach ($delegate->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { + $methodName = $method->name; + $getter = preg_match('/^(get|has|is)/', $methodName); + $setter = preg_match('/^(set|unset)/', $methodName); + $argPrototypeList = []; + $argNameList = []; + $needsImplementation = false; + foreach ($method->getParameters() as $arg) { + $argDescription = '$' . $arg->name; + $argNameList[] = $argDescription; + if ($arg->isOptional()) { + $argDescription = $argDescription . ' = ' . str_replace("\n", "", var_export($arg->getDefaultValue(), true)); + // We will create wrapper methods for any method that + // has default parameters. + $needsImplementation = true; + } + $argPrototypeList[] = $argDescription; + } + $argPrototypeString = implode(', ', $argPrototypeList); + $argNameListString = implode(', ', $argNameList); + + if ($methodName[0] != '_') { + $methodDescriptions[] = "@method $methodName($argPrototypeString)"; + + if ($getter) { + $immediateMethods[] = " public function $methodName($argPrototypeString)\n {\n return \$this->delegate->$methodName($argNameListString);\n }"; + } elseif ($setter) { + $immediateMethods[] = " public function $methodName($argPrototypeString)\n {\n \$this->delegate->$methodName($argNameListString);\n return \$this;\n }"; + } elseif ($needsImplementation) { + // Include an implementation for the wrapper method if necessary + $methodImplementations[] = " protected function _$methodName($argPrototypeString)\n {\n \$this->delegate->$methodName($argNameListString);\n }"; + } + } + } + + $classNameParts = explode('\\', $this->className); + $delegate = array_pop($classNameParts); + $delegateNamespace = implode('\\', $classNameParts); + + if (empty($this->wrapperClassName)) { + $this->wrapperClassName = $delegate; + } + + $replacements['{delegateNamespace}'] = $delegateNamespace; + $replacements['{delegate}'] = $delegate; + $replacements['{wrapperClassName}'] = $this->wrapperClassName; + $replacements['{taskname}'] = "task$delegate"; + $replacements['{methodList}'] = $leadingCommentChars . implode("\n$leadingCommentChars", $methodDescriptions); + $replacements['{immediateMethods}'] = "\n\n" . implode("\n\n", $immediateMethods); + $replacements['{methodImplementations}'] = "\n\n" . implode("\n\n", $methodImplementations); + + $template = file_get_contents(__DIR__ . '/../../../data/Task/Development/GeneratedWrapper.tmpl'); + $template = str_replace(array_keys($replacements), array_values($replacements), $template); + + // Returning data in the $message will cause it to be printed. + return Result::success($this, $template); + } +}