-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[POC] Generate node types command #157
base: 3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should ask for confirmation -- later on, if we add property and child item support we could also show a diff as @dbu suggested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the diff thing is not that important i think. removing is not possible anyways, so we can only add. and adding a nodetype that already exists is ok in cnd, its just a noop. |
||
} | ||
|
||
$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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can get a namespace URL from the bundle container extension, which is quite neat. I also use the name of the extension as the alias, we could also use the bundle name (normally the difference between |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commands can be services since 2.4. this could move into a constructor as default value, with the option to override in configuration.