Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / PSR4.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Pharborist\Objects\ClassNode;
8 use Pharborist\RootNode;
9 use Pharborist\WhitespaceNode;
10
11 /**
12  * @Converter(
13  *  id = "PSR4",
14  *  description = @Translation("Moves classes into PSR-4 directory structure.")
15  * )
16  */
17 class PSR4 extends ConverterBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function isExecutable(TargetInterface $target) {
23     return (boolean) $target->getIndexer('class')->getQuery()->countQuery()->execute();
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function convert(TargetInterface $target) {
30     $target
31       ->getIndexer('class')
32       ->getAll()
33       ->each(function(ClassNode $class) use ($target) {
34         $this->writeClass($target, self::toPSR4($target, $class));
35       });
36   }
37
38   /**
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
42    * *copy* of it.
43    *
44    * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
45    *  The module which will own the class.
46    * @param \Pharborist\ClassNode $class
47    *  The class to modify.
48    *
49    * @return \Pharborist\ClassNode
50    *  The modified class, returned for convenience.
51    */
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);
56
57     return $class;
58   }
59
60 }