Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Fixer / CreateClass.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer\CreateClass.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer;
9
10 use Drupal\drupalmoduleupgrader\FixerBase;
11 use Pharborist\DocCommentNode;
12 use Pharborist\Objects\ClassNode;
13 use Pharborist\Parser;
14 use Pharborist\RootNode;
15 use Pharborist\Token;
16 use Symfony\Component\Filesystem\Filesystem;
17
18 /**
19  * @Fixer(
20  *  id = "create_class"
21  * )
22  */
23 class CreateClass extends FixerBase {
24
25   /**
26    * @var \Symfony\Component\Filesystem\Filesystem
27    */
28   protected $fs;
29
30   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
31     parent::__construct($configuration, $plugin_id, $plugin_definition);
32     $this->fs = new Filesystem();
33   }
34
35   public function execute() {
36     $ns = $this->extractNS($this->configuration['class']);
37     $class = $this->extractLocal($this->configuration['class']);
38
39     $doc = RootNode::create($ns);
40     $ns = $doc->getNamespace($ns);
41     Token::newline()->insertBefore($ns);
42     Token::newline()->appendTo($ns);
43     $class = ClassNode::create($class);
44
45     if ($parent = $this->configuration['parent']) {
46       Parser::parseSnippet('use ' . ltrim($parent, '\\') . ';')
47         ->appendTo($ns)
48         ->after(Token::newline());
49       $class->setExtends($this->extractLocal($parent));
50     }
51
52     $interfaces = (array) $this->configuration['interfaces'];
53     foreach ($interfaces as $interface) {
54       Parser::parseSnippet('use ' . ltrim($interface, '\\') . ';')
55         ->appendTo($ns)
56         ->after(Token::newline());
57     }
58     $class->setImplements(array_map([ $this, 'extractLocal' ], $interfaces));
59
60     if (isset($this->configuration['doc'])) {
61       $class->setDocComment(DocCommentNode::create($this->configuration['doc']));
62     }
63
64     $class->appendTo($ns)->before(Token::newline());
65
66     $destination = $this->getUnaliasedPath($this->configuration['destination']);
67     $dir = subStr($destination, 0, strrPos($destination, '/'));
68     $this->fs->mkdir($dir);
69     file_put_contents($destination, $doc->getText());
70     // Need to store the class' local name as its index identifier because
71     // \Pharborist\Filter::isClass() doesn't support lookup by qualified path.
72     $this->target->getIndexer('class')->addFile($destination);
73   }
74
75   protected function extractLocal($path) {
76     return subStr($path, strrPos($path, '\\') + 1);
77   }
78
79   protected function extractNS($path) {
80     $path = ltrim($path, '\\');
81     return subStr($path, 0, strrPos($path, '\\'));
82   }
83
84 }