Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Generator / PluginBlockGenerator.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Generator\PluginBlockGenerator.
6  */
7
8 namespace Drupal\Console\Generator;
9
10 use Drupal\Console\Core\Generator\Generator;
11 use Drupal\Console\Extension\Manager;
12
13 class PluginBlockGenerator extends Generator
14 {
15     /**
16      * @var Manager
17      */
18     protected $extensionManager;
19
20     /**
21      * PermissionGenerator constructor.
22      *
23      * @param Manager $extensionManager
24      */
25     public function __construct(
26         Manager $extensionManager
27     ) {
28         $this->extensionManager = $extensionManager;
29     }
30
31     /**
32      * Generator Plugin Block.
33      *
34      * @param $module
35      * @param $class_name
36      * @param $label
37      * @param $plugin_id
38      * @param $services
39      */
40     public function generate($module, $class_name, $label, $plugin_id, $services, $inputs)
41     {
42         // Consider the type when determining a default value. Figure out what
43         // the code looks like for the default value tht we need to generate.
44         foreach ($inputs as &$input) {
45             $default_code = '$this->t(\'\')';
46             if ($input['default_value'] == '') {
47                 switch ($input['type']) {
48                 case 'checkbox':
49                 case 'number':
50                 case 'weight':
51                 case 'radio':
52                     $default_code = 0;
53                     break;
54
55                 case 'radios':
56                 case 'checkboxes':
57                     $default_code = 'array()';
58                     break;
59                 }
60             } elseif (substr($input['default_value'], 0, 1) == '$') {
61                 // If they want to put in code, let them, they're programmers.
62                 $default_code = $input['default_value'];
63             } elseif (is_numeric($input['default_value'])) {
64                 $default_code = $input['default_value'];
65             } elseif (preg_match('/^(true|false)$/i', $input['default_value'])) {
66                 // Coding Standards
67                 $default_code = strtoupper($input['default_value']);
68             } else {
69                 $default_code = '$this->t(\'' . $input['default_value'] . '\')';
70             }
71             $input['default_code'] = $default_code;
72         }
73
74         $parameters = [
75           'module' => $module,
76           'class_name' => $class_name,
77           'label' => $label,
78           'plugin_id' => $plugin_id,
79           'services' => $services,
80           'inputs' => $inputs,
81         ];
82
83         $this->renderFile(
84             'module/src/Plugin/Block/block.php.twig',
85             $this->extensionManager->getPluginPath($module, 'Block').'/'.$class_name.'.php',
86             $parameters
87         );
88     }
89 }