diff --git a/WrapperBundle/DependencyInjection/Configuration.php b/WrapperBundle/DependencyInjection/Configuration.php index e469c45..4bc5178 100644 --- a/WrapperBundle/DependencyInjection/Configuration.php +++ b/WrapperBundle/DependencyInjection/Configuration.php @@ -17,8 +17,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('ezobject_wrapper'); + $treeBuilder = new TreeBuilder('ezobject_wrapper'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() diff --git a/WrapperBundle/Resources/config/services.yml b/WrapperBundle/Resources/config/services.yml index 5e4dad3..4c97d93 100644 --- a/WrapperBundle/Resources/config/services.yml +++ b/WrapperBundle/Resources/config/services.yml @@ -21,6 +21,11 @@ services: twig.extension.ezobject_wrapper: class: Kaliop\eZObjectWrapperBundle\Twig\eZObjectWrapperExtension - arguments: ['@service_container'] + arguments: + - '@ezpublish.api.repository' + - '@ezpublish.view_manager' tags: - { name: twig.extension } + + # autowiring + Kaliop\eZObjectWrapperBundle\Core\EntityManager: '@ezobject_wrapper.entity_manager' \ No newline at end of file diff --git a/WrapperBundle/Twig/eZObjectWrapperExtension.php b/WrapperBundle/Twig/eZObjectWrapperExtension.php index 11a4cb1..81fc914 100644 --- a/WrapperBundle/Twig/eZObjectWrapperExtension.php +++ b/WrapperBundle/Twig/eZObjectWrapperExtension.php @@ -2,46 +2,78 @@ namespace Kaliop\eZObjectWrapperBundle\Twig; +use eZ\Publish\API\Repository\Repository; +use eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; -class eZObjectWrapperExtension extends \Twig_Extension +class eZObjectWrapperExtension extends AbstractExtension { - private $container; + /** + * @var Repository + */ + private $repository; + + /** + * @var ViewManagerInterface + */ + private $viewManager; /** - * @param ContainerInterface $container + * @param Repository $repository + * @param ViewManagerInterface $viewManager */ - public function __construct(ContainerInterface $container) + public function __construct(Repository $repository, ViewManagerInterface $viewManager) { - $this->container = $container; + $this->repository = $repository; + $this->viewManager = $viewManager; } + /** + * @inheritDoc + */ public function getFunctions() { return array( - new \Twig_SimpleFunction('render_location', array($this,'renderLocation'), array('is_safe' => array('html'))), + new TwigFunction('render_location', array($this,'renderLocation'), array('is_safe' => array('html'))), + new TwigFunction('render_content', array($this,'renderContent'), array('is_safe' => array('html'))), ); } /** * Render a location according to the viewType and the ContentType set in override.yml, without doing a sub-request. - * Note: we can NOT inject directly the 'ezpublish.controller.content.view' service because it generates a ServiceCircularReferenceException * * @param int $locationID * @param string $viewType * @param array $params + * * @return string generally it is safe html and does not need to be further encoded/escaped */ public function renderLocation($locationID, $viewType, $params = array()) { - $rendering = $this->container->get('ezpublish.controller.content.view') - ->viewLocation($locationID, $viewType, false, $params); - - return $rendering->getContent(); + return $this->viewManager->renderLocation( + $this->repository->getLocationService()->loadLocation($locationID), + $viewType, + $params + ); } - public function getName() + /** + * Render a content according to the viewType and the ContentType set in override.yml, without doing a sub-request. + * + * @param int $contentID + * @param string $viewType + * @param array $params + * + * @return string generally it is safe html and does not need to be further encoded/escaped + */ + public function renderContent($contentID, $viewType, $params = array()) { - return 'ezobject_wrapper_extension'; + return $this->viewManager->renderContent( + $this->repository->getContentService()->loadContent($contentID), + $viewType, + $params + ); } } \ No newline at end of file diff --git a/composer.json b/composer.json index b27cbe5..efaa1f9 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": ">=5.6", - "ezsystems/ezpublish-kernel": ">=5.4|>=2014.11" + "ezsystems/ezpublish-kernel": ">=6.0" }, "require-dev": { "codeclimate/php-test-reporter": "~0.4",