5 * Contains \Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer\CreateClass.
8 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer;
10 use Drupal\drupalmoduleupgrader\FixerBase;
11 use Pharborist\DocCommentNode;
12 use Pharborist\Objects\ClassNode;
13 use Pharborist\Parser;
14 use Pharborist\RootNode;
16 use Symfony\Component\Filesystem\Filesystem;
23 class CreateClass extends FixerBase {
26 * @var \Symfony\Component\Filesystem\Filesystem
30 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
31 parent::__construct($configuration, $plugin_id, $plugin_definition);
32 $this->fs = new Filesystem();
35 public function execute() {
36 $ns = $this->extractNS($this->configuration['class']);
37 $class = $this->extractLocal($this->configuration['class']);
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);
45 if ($parent = $this->configuration['parent']) {
46 Parser::parseSnippet('use ' . ltrim($parent, '\\') . ';')
48 ->after(Token::newline());
49 $class->setExtends($this->extractLocal($parent));
52 $interfaces = (array) $this->configuration['interfaces'];
53 foreach ($interfaces as $interface) {
54 Parser::parseSnippet('use ' . ltrim($interface, '\\') . ';')
56 ->after(Token::newline());
58 $class->setImplements(array_map([ $this, 'extractLocal' ], $interfaces));
60 if (isset($this->configuration['doc'])) {
61 $class->setDocComment(DocCommentNode::create($this->configuration['doc']));
64 $class->appendTo($ns)->before(Token::newline());
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);
75 protected function extractLocal($path) {
76 return subStr($path, strrPos($path, '\\') + 1);
79 protected function extractNS($path) {
80 $path = ltrim($path, '\\');
81 return subStr($path, 0, strrPos($path, '\\'));