3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Pharborist\Objects\ClassNode;
8 use Pharborist\RootNode;
9 use Pharborist\WhitespaceNode;
14 * description = @Translation("Moves classes into PSR-4 directory structure.")
17 class PSR4 extends ConverterBase {
22 public function isExecutable(TargetInterface $target) {
23 return (boolean) $target->getIndexer('class')->getQuery()->countQuery()->execute();
29 public function convert(TargetInterface $target) {
33 ->each(function(ClassNode $class) use ($target) {
34 $this->writeClass($target, self::toPSR4($target, $class));
39 * Utility method to PSR4-ify a class. It'll move the class into its own file
40 * in the given module's namespace. The class is modified in-place, so you
41 * should clone it before calling this function if you want to make a PSR-4
44 * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
45 * The module which will own the class.
46 * @param \Pharborist\ClassNode $class
47 * The class to modify.
49 * @return \Pharborist\ClassNode
50 * The modified class, returned for convenience.
52 public static function toPSR4(TargetInterface $target, ClassNode $class) {
53 $ns = 'Drupal\\' . $target->id();
54 RootNode::create($ns)->getNamespace($ns)->append($class->remove());
55 WhitespaceNode::create("\n\n")->insertBefore($class);