comparison vendor/chi-teck/drupal-code-generator/src/Helper/Dumper.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace DrupalCodeGenerator\Helper;
4
5 use DrupalCodeGenerator\Utils;
6 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
7 use Symfony\Component\Console\Helper\Helper;
8 use Symfony\Component\Console\Input\InputInterface;
9 use Symfony\Component\Console\Output\OutputInterface;
10 use Symfony\Component\Console\Question\ConfirmationQuestion;
11 use Symfony\Component\Filesystem\Filesystem;
12
13 /**
14 * Output dumper form generators.
15 */
16 class Dumper extends Helper {
17
18 /**
19 * The file system utility.
20 *
21 * @var \Symfony\Component\Filesystem\Filesystem
22 */
23 public $filesystem;
24
25 /**
26 * Input instance.
27 *
28 * @var \Symfony\Component\Console\Input\InputInterface
29 */
30 protected $input;
31
32 /**
33 * Output instance.
34 *
35 * @var \Symfony\Component\Console\Output\OutputInterface
36 */
37 protected $output;
38
39 /**
40 * Replace flag.
41 *
42 * @var bool
43 */
44 protected $replace;
45
46 /**
47 * Constructs a generator command.
48 *
49 * @param \Symfony\Component\Filesystem\Filesystem $filesystem
50 * The file system utility.
51 * @param bool $replace
52 * (optional) Indicates weather or not existing files can be replaced.
53 */
54 public function __construct(Filesystem $filesystem, $replace = NULL) {
55 $this->filesystem = $filesystem;
56 $this->replace = $replace;
57 }
58
59 /**
60 * {@inheritdoc}
61 */
62 public function getName() {
63 return 'dcg_dumper';
64 }
65
66 /**
67 * Dumps the generated code to file system.
68 *
69 * @param \Symfony\Component\Console\Input\InputInterface $input
70 * Input instance.
71 * @param \Symfony\Component\Console\Output\OutputInterface $output
72 * Output instance.
73 *
74 * @return array
75 * List of created or updated files.
76 */
77 public function dump(InputInterface $input, OutputInterface $output) {
78 $this->input = $input;
79 $this->output = $output;
80 $formatter_style = new OutputFormatterStyle('black', 'cyan', []);
81 $this->output->getFormatter()->setStyle('title', $formatter_style);
82
83 $interactive = $input->isInteractive();
84
85 // NULL means we should ask user for confirmation.
86 if ($this->replace !== NULL) {
87 $input->setInteractive(FALSE);
88 }
89
90 /** @var \DrupalCodeGenerator\Command\GeneratorInterface $command */
91 $command = $this->getHelperSet()->getCommand();
92
93 $dumped_files = $this->doDump($command->getAssets(), $command->getDirectory());
94
95 $input->setInteractive($interactive);
96 return $dumped_files;
97 }
98
99 /**
100 * Dumps assets.
101 *
102 * @param \DrupalCodeGenerator\Asset[] $assets
103 * Files to dump.
104 * @param string $directory
105 * Directory where to dump the assets.
106 *
107 * @return array
108 * List of created or updated assets.
109 */
110 protected function doDump(array $assets, $directory) {
111 $dumped_files = [];
112
113 foreach ($assets as $asset) {
114
115 $content = $asset->getContent();
116 $path = $asset->getPath();
117
118 $file_path = "$directory/$path";
119 if ($this->filesystem->exists($file_path) && !$asset->isDirectory()) {
120 $action = $asset->getAction();
121 if ($action == 'replace') {
122 $question_text = sprintf('<info>The file <comment>%s</comment> already exists. Would you like to replace it?</info> [<comment>Yes</comment>]:', $file_path);
123 if (!$this->confirm($question_text)) {
124 continue;
125 }
126 }
127 else {
128 $original_content = file_get_contents($file_path);
129 if ($action == 'append') {
130 $header_size = $asset->getHeaderSize();
131 // Do not remove header if original file is empty.
132 if ($original_content && $header_size > 0) {
133 $content = Utils::removeHeader($content, $header_size);
134 }
135 $content = $original_content . "\n" . $content;
136 }
137 elseif (is_callable($action)) {
138 $content = $action($original_content, $content);
139 }
140 else {
141 throw new \LogicException("Unsupported action: $action.");
142 }
143 }
144 }
145
146 // Default mode for all parent directories is 0777. It can be modified by
147 // changing umask.
148 $mode = $asset->getMode();
149
150 // Save data to file system.
151 if ($asset->isDirectory()) {
152 $this->filesystem->mkdir([$file_path], $mode);
153 }
154 else {
155 $this->filesystem->dumpFile($file_path, $content);
156 $this->filesystem->chmod($file_path, $mode);
157 }
158
159 $dumped_files[] = $asset->getPath();
160 }
161
162 return $dumped_files;
163 }
164
165 /**
166 * Asks a user for confirmation.
167 *
168 * @param string $question_text
169 * The question to ask to the user.
170 *
171 * @return bool
172 * User confirmation.
173 */
174 protected function confirm($question_text) {
175 $question_text = "\n $question_text\n ➤ ";
176 // If the input is not interactive print the question with default answer.
177 if ($this->replace !== NULL) {
178 $this->output->writeln($question_text . ($this->replace ? 'Yes' : 'No'));
179 }
180 $question = new ConfirmationQuestion($question_text, $this->replace !== FALSE);
181 /** @var \Symfony\Component\Console\Helper\QuestionHelper $question_helper */
182 $question_helper = $this->getHelperSet()->get('question');
183 return $question_helper->ask($this->input, $this->output, $question);
184 }
185
186 }