Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / consolidation / robo / src / Task / Development / GenerateTask.php
1 <?php
2 namespace Robo\Task\Development;
3
4 use Robo\Task\BaseTask;
5 use Robo\Result;
6
7 /**
8  * Generate a Robo Task that is a wrapper around an existing class.
9  *
10  * ``` php
11  * <?php
12  * $this->taskGenerateTask('Symfony\Component\Filesystem\Filesystem', 'FilesystemStack')
13  *   ->run();
14  * ```
15  */
16 class GenerateTask extends BaseTask
17 {
18     /**
19      * @var string
20      */
21     protected $className;
22
23     /**
24      * @var string
25      */
26     protected $wrapperClassName;
27
28     /**
29      * @param string $className
30      * @param string $wrapperClassName
31      */
32     public function __construct($className, $wrapperClassName = '')
33     {
34         $this->className = $className;
35         $this->wrapperClassName = $wrapperClassName;
36     }
37
38     /**
39      * {@inheritdoc}
40      */
41     public function run()
42     {
43         $delegate = new \ReflectionClass($this->className);
44         $replacements = [];
45
46         $leadingCommentChars = " * ";
47         $methodDescriptions = [];
48         $methodImplementations = [];
49         $immediateMethods = [];
50         foreach ($delegate->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
51             $methodName = $method->name;
52             $getter = preg_match('/^(get|has|is)/', $methodName);
53             $setter = preg_match('/^(set|unset)/', $methodName);
54             $argPrototypeList = [];
55             $argNameList = [];
56             $needsImplementation = false;
57             foreach ($method->getParameters() as $arg) {
58                 $argDescription = '$' . $arg->name;
59                 $argNameList[] = $argDescription;
60                 if ($arg->isOptional()) {
61                     $argDescription = $argDescription . ' = ' . str_replace("\n", "", var_export($arg->getDefaultValue(), true));
62                     // We will create wrapper methods for any method that
63                     // has default parameters.
64                     $needsImplementation = true;
65                 }
66                 $argPrototypeList[] = $argDescription;
67             }
68             $argPrototypeString = implode(', ', $argPrototypeList);
69             $argNameListString = implode(', ', $argNameList);
70
71             if ($methodName[0] != '_') {
72                 $methodDescriptions[] = "@method $methodName($argPrototypeString)";
73
74                 if ($getter) {
75                     $immediateMethods[] = "    public function $methodName($argPrototypeString)\n    {\n        return \$this->delegate->$methodName($argNameListString);\n    }";
76                 } elseif ($setter) {
77                     $immediateMethods[] = "    public function $methodName($argPrototypeString)\n    {\n        \$this->delegate->$methodName($argNameListString);\n        return \$this;\n    }";
78                 } elseif ($needsImplementation) {
79                     // Include an implementation for the wrapper method if necessary
80                     $methodImplementations[] = "    protected function _$methodName($argPrototypeString)\n    {\n        \$this->delegate->$methodName($argNameListString);\n    }";
81                 }
82             }
83         }
84
85         $classNameParts = explode('\\', $this->className);
86         $delegate = array_pop($classNameParts);
87         $delegateNamespace = implode('\\', $classNameParts);
88
89         if (empty($this->wrapperClassName)) {
90             $this->wrapperClassName = $delegate;
91         }
92
93         $replacements['{delegateNamespace}'] = $delegateNamespace;
94         $replacements['{delegate}'] = $delegate;
95         $replacements['{wrapperClassName}'] = $this->wrapperClassName;
96         $replacements['{taskname}'] = "task$delegate";
97         $replacements['{methodList}'] = $leadingCommentChars . implode("\n$leadingCommentChars", $methodDescriptions);
98         $replacements['{immediateMethods}'] = "\n\n" . implode("\n\n", $immediateMethods);
99         $replacements['{methodImplementations}'] = "\n\n" . implode("\n\n", $methodImplementations);
100
101         $template = file_get_contents(__DIR__ . '/../../../data/Task/Development/GeneratedWrapper.tmpl');
102         $template = str_replace(array_keys($replacements), array_values($replacements), $template);
103
104         // Returning data in the $message will cause it to be printed.
105         return Result::success($this, $template);
106     }
107 }