Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / drupal / console-core / src / Generator / Generator.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Generator\Generator.
6  */
7
8 namespace Drupal\Console\Core\Generator;
9
10 use Drupal\Console\Core\Utils\TwigRenderer;
11 use Drupal\Console\Core\Utils\FileQueue;
12 use Drupal\Console\Core\Utils\CountCodeLines;
13 use Drupal\Console\Core\Utils\DrupalFinder;
14 use Drupal\Console\Core\Style\DrupalStyle;
15
16 /**
17  * Class Generator
18  *
19  * @package Drupal\Console\Core\Generator
20  */
21 abstract class Generator
22 {
23     /**
24      * @var TwigRenderer
25      */
26     protected $renderer;
27
28     /**
29      * @var FileQueue
30      */
31     protected $fileQueue;
32
33     /**
34      * @var CountCodeLines
35      */
36     protected $countCodeLines;
37
38     /**
39      * @var DrupalFinder
40      */
41     protected $drupalFinder;
42
43     /**
44      * @var DrupalStyle
45      */
46     protected $io;
47
48     /**
49      * @param $renderer
50      */
51     public function setRenderer(TwigRenderer $renderer)
52     {
53         $this->renderer = $renderer;
54     }
55
56     /**
57      * @param $fileQueue
58      */
59     public function setFileQueue(FileQueue $fileQueue)
60     {
61         $this->fileQueue = $fileQueue;
62     }
63
64     /**
65      * @param $countCodeLines
66      */
67     public function setCountCodeLines(CountCodeLines $countCodeLines)
68     {
69         $this->countCodeLines = $countCodeLines;
70     }
71
72     /**
73      * @param DrupalFinder $drupalFinder
74      */
75     public function setDrupalFinder($drupalFinder)
76     {
77         $this->drupalFinder = $drupalFinder;
78     }
79
80     /**
81      * @return \Drupal\Console\Core\Style\DrupalStyle
82      */
83     public function getIo() {
84         return $this->io;
85     }
86
87     /**
88      * @param \Drupal\Console\Core\Style\DrupalStyle $io
89      */
90     public function setIo($io) {
91         $this->io = $io;
92     }
93
94     /**
95      * @param string $template
96      * @param string $target
97      * @param array  $parameters
98      * @param null   $flag
99      *
100      * @return bool
101      */
102     protected function renderFile(
103         $template,
104         $target,
105         $parameters = [],
106         $flag = null
107     ) {
108         if (!is_dir(dirname($target))) {
109             if (!mkdir(dirname($target), 0777, true)) {
110                 throw new \InvalidArgumentException(
111                     sprintf(
112                         'Path "%s" is invalid. You need to provide a valid path.',
113                         dirname($target)
114                     )
115                 );
116             }
117         }
118
119         $currentLine = 0;
120         if (!empty($flag) && file_exists($target)) {
121             $currentLine = count(file($target));
122         }
123         $content = $this->renderer->render($template, $parameters);
124
125         if (file_put_contents($target, $content, $flag)) {
126             $this->fileQueue->addFile($target);
127
128             $newCodeLine = count(file($target));
129
130             if ($currentLine > 0) {
131                 $newCodeLine = ($newCodeLine-$currentLine);
132             }
133
134             $this->countCodeLines->addCountCodeLines($newCodeLine);
135
136             return true;
137         }
138
139         return false;
140     }
141
142     public function addSkeletonDir($skeletonDir)
143     {
144         $this->renderer->addSkeletonDir($skeletonDir);
145     }
146 }