1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Console;
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Console\Exception\GenerationDirectoryAccessException;
use Magento\Framework\Console\GenerationDirectoryAccess;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Phrase;
use Magento\Setup\Console\Command\DiCompileCommand;
use Magento\Setup\Mvc\Bootstrap\InitParamListener;
use Symfony\Component\Console\Input\ArgvInput;
use Zend\ServiceManager\ServiceManager;
/**
* Class prepares folders for code generation
*/
class CompilerPreparation
{
/**
* @var ServiceManager
*/
private $serviceManager;
/**
* @var ArgvInput
*/
private $input;
/**
* @var File
*/
private $filesystemDriver;
/**
* @var GenerationDirectoryAccess
*/
private $generationDirectoryAccess;
/**
* @param ServiceManager $serviceManager
* @param ArgvInput $input
* @param File $filesystemDriver
*/
public function __construct(
ServiceManager $serviceManager,
ArgvInput $input,
File $filesystemDriver
) {
$this->serviceManager = $serviceManager;
$this->input = $input;
$this->filesystemDriver = $filesystemDriver;
}
/**
* Determine whether a CLI command is for compilation, and if so, clear the directory.
*
* @throws GenerationDirectoryAccessException If generation directory is read-only
* @return void
*/
public function handleCompilerEnvironment()
{
$compilationCommands = $this->getCompilerInvalidationCommands();
$cmdName = $this->input->getFirstArgument();
$isHelpOption = $this->input->hasParameterOption('--help') || $this->input->hasParameterOption('-h');
if (!in_array($cmdName, $compilationCommands) || $isHelpOption) {
return;
}
if (!$this->getGenerationDirectoryAccess()->check()) {
throw new GenerationDirectoryAccessException();
}
$mageInitParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM);
$mageDirs = isset($mageInitParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS])
? $mageInitParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]
: [];
$directoryList = new DirectoryList(BP, $mageDirs);
$compileDirList = [
$directoryList->getPath(DirectoryList::GENERATED_CODE),
$directoryList->getPath(DirectoryList::GENERATED_METADATA),
];
foreach ($compileDirList as $compileDir) {
if ($this->filesystemDriver->isExists($compileDir)) {
$this->filesystemDriver->deleteDirectory($compileDir);
}
}
}
/**
* Retrieves command list with commands which invalidates compiler
*
* @return array
*/
private function getCompilerInvalidationCommands()
{
return [
DiCompileCommand::NAME,
'module:disable',
'module:enable',
'module:uninstall',
];
}
/**
* Retrieves generation directory access checker.
*
* @return GenerationDirectoryAccess the generation directory access checker
*/
private function getGenerationDirectoryAccess()
{
if (null === $this->generationDirectoryAccess) {
$this->generationDirectoryAccess = new GenerationDirectoryAccess($this->serviceManager);
}
return $this->generationDirectoryAccess;
}
}