-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathCndGenerateCommand.php
More file actions
96 lines (78 loc) · 3.66 KB
/
CndGenerateCommand.php
File metadata and controls
96 lines (78 loc) · 3.66 KB
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
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Bundle\PHPCRBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\Console\Input\InputArgument;
class CndGenerateCommand extends ContainerAwareCommand
{
public function configure()
{
$this->setName('doctrine:phpcr:generate:cnd');
$this->addArgument('bundleName', InputArgument::REQUIRED, 'Generate node types for this bundle');
}
public function execute(InputInterface $input, OutputInterface $output)
{
$cndPath = 'Resources/config/phpcr-node-types';
$bundleName = $input->getArgument('bundleName');
$manager = $this->getContainer()->get('doctrine_phpcr');
$kernel = $this->getContainer()->get('kernel');
$fs = new Filesystem();
$metas = $manager->getManager()->getMetadataFactory()->getAllMetadata();
$bundle = $kernel->getBundle($bundleName);
list($ntNs, $ntNsAlias) = $this->getNsInfo($bundle);
foreach ($metas as $meta) {
if (0 !== strpos($meta->name, $bundle->getNamespace())) {
continue;
}
$ntName = $meta->getReflectionClass()->getShortName();
$destPath = sprintf('%s/%s/%s.%s.%s', $bundle->getPath(), $cndPath, $ntNsAlias, $ntName, 'cnd');
$dirPath = dirname($destPath);
if (!file_exists($dirPath) ){
$output->writeln('Creating directory: <info>%s</info>', $dirPath);
$fs->mkdir($dirPath);
}
if (file_exists($destPath)) {
$fs->remove($destPath);
}
$template = array(
sprintf('// File generated by the DoctrinePHPCRBundle %s', date('C')),
sprintf('<%s = \'%s\'>', $ntNsAlias, $ntNs),
sprintf('[%s:%s] > nt:unstructured', $ntNsAlias, $ntName),
);
$output->writeln(sprintf('Writing CND file: <info>%s</info>', $destPath));
file_put_contents($destPath, join("\n", $template));
}
}
public function getNsInfo(BundleInterface $bundle)
{
$extension = $bundle->getContainerExtension();
if (null === $extension) {
throw new \Exception(sprintf(
'No container extension defined for bundle "%s"' . PHP_EOL .
'A container extension is mandatory for CND generation',
$bundle->getName()
));
}
return array($extension->getNamespace(), $extension->getAlias());
}
}