...
This receipt shows how to setup a simple symfony bundle with a commandline script using the public API. The command is executable within the appezpublish/console and dumps a content object for a given content id.
...
cd <ezpublish installation root>
Generate a new Bundle
php appezpublish/console generate:bundle
Now follow the instructions. This will create a bundle EzSystems/CookBookBundle in the src directory of the installation root (Note: this bundle exists already so a different namespace will be required).
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?php /** * File containing the CookBookCommand class. * * @copyright Copyright (C) 2012 eZ Systems AS. All rights reserved. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 * @version //autogentag// */ namespace EzSystems\CookBookBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; class CookBookCommand extends ContainerAwareCommand { /** * This method override configures on input argument for the content id */ protected function configure() { $this->setName( 'cookbook:run' )->setDefinition( array( new InputArgument( 'contentId', InputArgument::REQUIRED, 'An existing content id' ) ) ); } /** * execute the command * @param InputInterface $input * @param OutputInterface $output */ protected function execute( InputInterface $input, OutputInterface $output ) { // fetch the input argument $contentId = $input->getArgument( 'contentId' ); // get the repository from the di container $repository = $this->getContainer()->get( 'ezpublish.api.repository' ); // get the content service from the repsitory $contentService = $repository->getContentService(); try { // print out the content info for the given content id print_r( $contentService->loadContentInfo( $contentId ) ); } catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e ) { // if the id is not found $output->writeln( "No content with id $contentId" ); } catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e ) { // not allowed to read this content $output->writeln( "Anonymous users are not allowed to read content with id $contentId" ); } } } |
run f.e.
php appezpublish/console cookbook:run 5
Info | ||
---|---|---|
| ||
...