1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\EasyExtendsBundle\Generator; |
13
|
|
|
|
14
|
|
|
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
class BundleGenerator implements GeneratorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $bundleTemplate; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->bundleTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/bundle/bundle.mustache'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata): void |
33
|
|
|
{ |
34
|
|
|
$this->generateBundleDirectory($output, $bundleMetadata); |
35
|
|
|
$this->generateBundleFile($output, $bundleMetadata); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param OutputInterface $output |
40
|
|
|
* @param BundleMetadata $bundleMetadata |
41
|
|
|
*/ |
42
|
|
|
protected function generateBundleDirectory(OutputInterface $output, BundleMetadata $bundleMetadata): void |
43
|
|
|
{ |
44
|
|
|
$directories = array( |
45
|
|
|
'', |
46
|
|
|
'Resources/config/serializer', |
47
|
|
|
'Resources/config/doctrine', |
48
|
|
|
'Resources/config/routing', |
49
|
|
|
'Resources/views', |
50
|
|
|
'Command', |
51
|
|
|
'DependencyInjection', |
52
|
|
|
'Entity', |
53
|
|
|
'Document', |
54
|
|
|
'PHPCR', |
55
|
|
|
'Controller', |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
foreach ($directories as $directory) { |
59
|
|
|
$dir = sprintf('%s/%s', $bundleMetadata->getExtendedDirectory(), $directory); |
60
|
|
|
if (!is_dir($dir)) { |
61
|
|
|
$output->writeln(sprintf(' > generating bundle directory <comment>%s</comment>', $dir)); |
62
|
|
|
mkdir($dir, 0755, true); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* @param BundleMetadata $bundleMetadata |
70
|
|
|
*/ |
71
|
|
|
protected function generateBundleFile(OutputInterface $output, BundleMetadata $bundleMetadata): void |
72
|
|
|
{ |
73
|
|
|
$application = explode('\\', $bundleMetadata->getExtendedNamespace())[0]; |
74
|
|
|
$file = sprintf( |
75
|
|
|
'%s/%s%s.php', |
76
|
|
|
$bundleMetadata->getExtendedDirectory(), |
77
|
|
|
$application, |
78
|
|
|
$bundleMetadata->getName() |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
if (is_file($file)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$output->writeln(sprintf(' > generating bundle file <comment>%s</comment>', $file)); |
86
|
|
|
|
87
|
|
|
$string = Mustache::replace($this->getBundleTemplate(), array( |
88
|
|
|
'application' => $application, |
89
|
|
|
'bundle' => $bundleMetadata->getName(), |
90
|
|
|
'namespace' => $bundleMetadata->getExtendedNamespace(), |
91
|
|
|
)); |
92
|
|
|
|
93
|
|
|
file_put_contents($file, $string); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function getBundleTemplate(): string |
100
|
|
|
{ |
101
|
|
|
return $this->bundleTemplate; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|