Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions Controller/JsloaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* This controller includes the correct twig file to bootstrap the javascript
Expand Down Expand Up @@ -48,6 +49,16 @@ class JsloaderController
*/
private $plainTextTypes;

/**
* @var array
*/
private $createRoutesForTypes;

/**
* @var ContainerInterface
*/
private $container;


/**
* Create the Controller
Expand All @@ -62,6 +73,7 @@ class JsloaderController
* @param Boolean $useCoffee whether assetic is set up to use coffee script
* @param Boolean $fixedToolbar whether the hallo toolbar is fixed or floating
* @param array $plainTextTypes RDFa types to edit in raw text only
* @param array $createRoutesForTypes types for which it is needed to create a route
* @param string $requiredRole
* @param SecurityContextInterface $securityContext
*/
Expand All @@ -72,24 +84,27 @@ public function __construct(
$useCoffee = false,
$fixedToolbar = true,
$plainTextTypes = array(),
$createRoutesForTypes = array(),
$requiredRole = "IS_AUTHENTICATED_ANONYMOUSLY",
SecurityContextInterface $securityContext = null
SecurityContextInterface $securityContext = null,
ContainerInterface $container
) {
$this->viewHandler = $viewHandler;
$this->stanbolUrl = $stanbolUrl;
$this->imageClass = $imageClass;
$this->coffee = $useCoffee;
$this->fixedToolbar = $fixedToolbar;
$this->plainTextTypes = $plainTextTypes;

$this->createRoutesForTypes = $createRoutesForTypes;
$this->requiredRole = $requiredRole;
$this->securityContext = $securityContext;
$this->container = $container;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would prefer you inject the parameters explicitly .. if you want you can inject them all as an array

}

/**
* Render js inclusion for create.js and dependencies and bootstrap code.
*
* THe hallo editor is bundled with create.js and available automatically.
* The hallo editor is bundled with create.js and available automatically.
* To use aloha, you need to download the zip, as explained in step 8 of
* the README.
*
Expand Down Expand Up @@ -120,12 +135,18 @@ public function includeJSFilesAction($editor = 'hallo')
throw new \InvalidArgumentException("Unknown editor '$editor' requested");
}



$view->setData(array(
'cmfCreateStanbolUrl' => $this->stanbolUrl,
'cmfCreateImageUploadEnabled' => (boolean) $this->imageClass,
'cmfCreateHalloFixedToolbar' => (boolean) $this->fixedToolbar,
'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes))
);
'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes),
'cmfCreateCreateRoutesTypes' => json_encode($this->createRoutesForTypes),
'cmfCreateLocales' => json_encode($this->container->getParameter('locales')),
'cmfCreateContentPrefix' => $this->container->getParameter('symfony_cmf_content.content_basepath'),
'cmfCreateRoutesPrefix' => $this->container->getParameter('symfony_cmf_routing_extra.routing_repositoryroot')
));

return $this->viewHandler->handle($view);
}
Expand Down
10 changes: 8 additions & 2 deletions Controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,22 @@ public function postDocumentAction(Request $request)
$this->performSecurityChecks();

$rdfType = trim($request->request->get('@type'), '<>');

$type = $this->typeFactory->getTypeByRdf($rdfType);

$result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_POST);
$result = null;
try {
$result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_POST);
} catch (\Exception $e) {
return Response::create("The document of type \"$rdfType\" could not be created: " . $e->getMessage(), 500);
}

if (!is_null($result)) {
$view = View::create($result)->setFormat('json');
return $this->viewHandler->handle($view, $request);
}

return Response::create('The document could not be created', 500);
return Response::create("The document \"$rdfType\" could not be created", 500);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just "The document '$rdfType' could not be created"

}

/**
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('rdfmapper')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->scalarNode('role')->defaultValue('IS_AUTHENTICATED_ANONYMOUSLY')->end()
->arrayNode('image')
->canBeUnset()
Expand All @@ -43,6 +47,10 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('create_routes_types')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('rdf_config_dirs')
->useAttributeAsKey('dir')
->prototype('scalar')->end()
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/SymfonyCmfCreateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter($this->getAlias().'.map', $config['map']);

$container->setParameter($this->getAlias().'.rdfmapper', $config['rdfmapper']);

$container->setParameter($this->getAlias().'.stanbol_url', $config['stanbol_url']);

$container->setParameter($this->getAlias().'.role', $config['role']);
Expand All @@ -54,6 +56,8 @@ public function load(array $configs, ContainerBuilder $container)
}
$container->setParameter($this->getAlias().'.plain_text_types', $config['plain_text_types']);

$container->setParameter($this->getAlias().'.create_routes_types', $config['create_routes_types']);

if ($config['auto_mapping']) {
foreach ($container->getParameter('kernel.bundles') as $class) {
$bundle = new \ReflectionClass($class);
Expand Down
33 changes: 33 additions & 0 deletions Mapper/RouteDoctrinePhpcrOdmMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Symfony\Cmf\Bundle\CreateBundle\Mapper;

use Midgard\CreatePHP\Mapper\DoctrinePhpcrOdmMapper;

use Midgard\CreatePHP\Entity\PropertyInterface;

use Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route;

/**
* Mapper to handle route documents stored in PHPCR-ODM.
*/
class RouteDoctrinePhpcrOdmMapper extends DoctrinePhpcrOdmMapper
{
public function setPropertyValue($object, PropertyInterface $property, $value)
{
if ($object instanceof Route && $property->getIdentifier() === 'locale') {
$object->setDefault('_locale', $value);
$object->setRequirement('_locale', $value);
return $object;
}
return parent::setPropertyValue($object, $property, $value);
}

public function getPropertyValue($object, PropertyInterface $property)
{
if ($object instanceof Route && $property->getIdentifier() === 'locale') {
return $object->getDefault('_locale');
}
return parent::getPropertyValue($object, $property);
}
}
64 changes: 64 additions & 0 deletions Metadata/ContainerRdfTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Symfony\Cmf\Bundle\CreateBundle\Metadata;

use Midgard\CreatePHP\RdfMapperInterface;
use Midgard\CreatePHP\Type\TypeInterface;

use Midgard\CreatePHP\Metadata\RdfTypeFactory;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Midgard\CreatePHP\Metadata\RdfDriverInterface;

/**
* Factory for createphp types based on class names. If available, the mappers
* for the requested class are loaded from the Symfony service container.
*/
class ContainerRdfTypeFactory extends RdfTypeFactory implements ContainerAwareInterface
{
/**
* @var array service names of the mappers
*/
private $mapperServices;

/**
* @var ContainerInterface
*/
protected $container;

/**
* @param RdfMapperInterface $defaultMapper the default mapper to use if there is no specific one
* @param RdfDriverInterface $driver the driver to load types from
* @param array $mappers rdf mappers per service name
*/
public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterface $driver, $mapperServices = array())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a array type hint for $mapperServices please

{
$this->mapperServices = $mapperServices;
parent::__construct($defaultMapper, $driver);
}

/**
* Get the mapper for type $name in the $symfony container, or the defaultMapper if there is no specific one
*
* @param string $name the type name for which to get the mapper
*
* @return RdfMapperInterface
*/
protected function getMapper($name)
{
if (isset($this->mapperServices[$name])) {
return $this->container->get($this->mapperServices[$name]);
}

return parent::getMapper($name);
}

/**
* @see ContainerAwareInterface::setContainer()
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}
7 changes: 7 additions & 0 deletions Resources/config/phpcr_odm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="symfony_cmf_create.phpcr_odm_mapper_class">Midgard\CreatePHP\Mapper\DoctrinePhpcrOdmMapper</parameter>
<parameter key="symfony_cmf_create.route_doctrine_phpcr_odm_mapper_class">Symfony\Cmf\Bundle\CreateBundle\Mapper\RouteDoctrinePhpcrOdmMapper</parameter>
</parameters>

<services>
Expand All @@ -16,5 +17,11 @@
<argument>null</argument>
</service>

<service id="symfony_cmf_create.route_doctrine_phpcr_odm_mapper" class="%symfony_cmf_create.route_doctrine_phpcr_odm_mapper_class%" public="true">
<argument>%symfony_cmf_create.map%</argument>
<argument type="service" id="doctrine_phpcr"/>
<argument>null</argument>
</service>

</services>
</container>
14 changes: 10 additions & 4 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<parameter key="symfony_cmf_create.jsloader.controller.class">Symfony\Cmf\Bundle\CreateBundle\Controller\JsloaderController</parameter>
<parameter key="symfony_cmf_create.rest.controller.class">Symfony\Cmf\Bundle\CreateBundle\Controller\RestController</parameter>
<parameter key="symfony_cmf_create.rdf_type_factory_class">Midgard\CreatePHP\Metadata\RdfTypeFactory</parameter>
<parameter key="symfony_cmf_create.container_rdf_type_factory_class">Symfony\Cmf\Bundle\CreateBundle\Metadata\ContainerRdfTypeFactory</parameter>
<parameter key="symfony_cmf_create.rest.handler_class">Midgard\CreatePHP\RestService</parameter>
</parameters>

Expand All @@ -19,32 +20,37 @@
<argument>%symfony_cmf_create.use_coffee%</argument>
<argument>%symfony_cmf_create.fixed_toolbar%</argument>
<argument>%symfony_cmf_create.plain_text_types%</argument>
<argument>%symfony_cmf_create.create_routes_types%</argument>
<argument>%symfony_cmf_create.role%</argument>
<argument type="service" id="security.context" on-invalid="ignore"/>
<argument type="service" id="service_container" />
</service>

<service id="symfony_cmf_create.rest.controller" class="%symfony_cmf_create.rest.controller.class%">
<argument type="service" id="fos_rest.view_handler"/>
<argument type="service" id="symfony_cmf_create.object_mapper"/>
<argument type="service" id="symfony_cmf_create.rdf_type_factory"/>
<argument type="service" id="symfony_cmf_create.container_rdf_type_factory"/>
<argument type="service" id="symfony_cmf_create.rest.handler"/>
<argument>%symfony_cmf_create.role%</argument>
<argument type="service" id="security.context" on-invalid="ignore"/>
</service>

<service id="symfony_cmf_create.twig_extension" class="Midgard\CreatePHP\Extension\Twig\CreatephpExtension">
<argument type="service" id="symfony_cmf_create.rdf_type_factory"/>
<argument type="service" id="symfony_cmf_create.container_rdf_type_factory"/>
<tag name="twig.extension"/>
</service>

<service id="symfony_cmf_create.rdf_driver" class="Midgard\CreatePHP\Metadata\RdfDriverXml">
<argument>%symfony_cmf_create.rdf_config_dirs%</argument>
</service>

<service id="symfony_cmf_create.rdf_type_factory" class="%symfony_cmf_create.rdf_type_factory_class%" public="true">
<service id="symfony_cmf_create.container_rdf_type_factory" class="%symfony_cmf_create.container_rdf_type_factory_class%" public="true">
<argument type="service" id="symfony_cmf_create.object_mapper"/>
<argument type="service" id="symfony_cmf_create.rdf_driver"/>
<argument>%symfony_cmf_create.map%</argument>
<argument>%symfony_cmf_create.rdfmapper%</argument>
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>

<service id="symfony_cmf_create.rest.handler" class="%symfony_cmf_create.rest.handler_class%" public="true">
Expand Down
Loading