You are here

public function CreateClass::execute in Drupal 7 to 8/9 Module Upgrader 8

Executes the plugin.

Overrides ExecutableInterface::execute

File

src/Plugin/DMU/Fixer/CreateClass.php, line 30

Class

CreateClass
Plugin annotation @Fixer( id = "create_class" )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer

Code

public function execute() {
  $ns = $this
    ->extractNS($this->configuration['class']);
  $class = $this
    ->extractLocal($this->configuration['class']);
  $doc = RootNode::create($ns);
  $ns = $doc
    ->getNamespace($ns);
  Token::newline()
    ->insertBefore($ns);
  Token::newline()
    ->appendTo($ns);
  $class = ClassNode::create($class);
  if ($parent = $this->configuration['parent']) {
    Parser::parseSnippet('use ' . ltrim($parent, '\\') . ';')
      ->appendTo($ns)
      ->after(Token::newline());
    $class
      ->setExtends($this
      ->extractLocal($parent));
  }
  $interfaces = (array) $this->configuration['interfaces'];
  foreach ($interfaces as $interface) {
    Parser::parseSnippet('use ' . ltrim($interface, '\\') . ';')
      ->appendTo($ns)
      ->after(Token::newline());
  }
  $class
    ->setImplements(array_map([
    $this,
    'extractLocal',
  ], $interfaces));
  if (isset($this->configuration['doc'])) {
    $class
      ->setDocComment(DocCommentNode::create($this->configuration['doc']));
  }
  $class
    ->appendTo($ns)
    ->before(Token::newline());
  $destination = $this
    ->getUnaliasedPath($this->configuration['destination']);
  $dir = substr($destination, 0, strrpos($destination, '/'));
  $this->fs
    ->mkdir($dir);
  file_put_contents($destination, $doc
    ->getText());

  // Need to store the class' local name as its index identifier because
  // \Pharborist\Filter::isClass() doesn't support lookup by qualified path.
  $this->target
    ->getIndexer('class')
    ->addFile($destination);
}