commit 2ebd499185f46d1dd9fd0522be7663b2ff1ea4aa
parent 695f72928e34ff35bf50d718ed04f9cecc98e941
Author: Felicitus <felicitus@felicitus.org>
Date: Wed, 27 May 2015 16:19:18 +0200
Added annotation to allow an entity to reflect to the target REST service.
Diffstat:
5 files changed, 54 insertions(+), 17 deletions(-)
diff --git a/src/PartKeepr/DoctrineReflectionBundle/Annotation/TargetService.php b/src/PartKeepr/DoctrineReflectionBundle/Annotation/TargetService.php
@@ -0,0 +1,16 @@
+<?php
+namespace PartKeepr\DoctrineReflectionBundle\Annotation;
+
+use Doctrine\ORM\Mapping\Annotation;
+
+/**
+ * @Annotation
+ * @Target("CLASS")
+ */
+final class TargetService implements Annotation
+{
+ /**
+ * @var string
+ */
+ public $uri;
+}
diff --git a/src/PartKeepr/DoctrineReflectionBundle/Resources/config/services.xml b/src/PartKeepr/DoctrineReflectionBundle/Resources/config/services.xml
@@ -8,6 +8,7 @@
<service id="doctrine_reflection_service" class="PartKeepr\DoctrineReflectionBundle\Services\ReflectionService">
<argument type="service" id="doctrine"/>
<argument type="service" id="templating"/>
+ <argument type="service" id="annotation_reader"/>
</service>
<service id="model_cache_warmer" class="PartKeepr\DoctrineReflectionBundle\CacheWarmer\ModelCacheWarmer">
diff --git a/src/PartKeepr/DoctrineReflectionBundle/Resources/views/model.js.twig b/src/PartKeepr/DoctrineReflectionBundle/Resources/views/model.js.twig
@@ -8,5 +8,10 @@ Ext.define('{{ className }}', {
}{% if not loop.last %},{% endif %}
{% endfor %}
- ]
+ ],
+
+ proxy: {
+ type: "PartKeeprREST",
+ url: '{{ uri|raw }}'
+ }
});
\ No newline at end of file
diff --git a/src/PartKeepr/DoctrineReflectionBundle/Services/ReflectionService.php b/src/PartKeepr/DoctrineReflectionBundle/Services/ReflectionService.php
@@ -2,6 +2,7 @@
namespace PartKeepr\DoctrineReflectionBundle\Services;
use Doctrine\Bundle\DoctrineBundle\Registry;
+use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;
use Symfony\Component\Templating\EngineInterface;
@@ -13,9 +14,12 @@ class ReflectionService {
protected $templateEngine;
- public function __construct (Registry $doctrine, EngineInterface $templateEngine) {
+ protected $reader;
+
+ public function __construct (Registry $doctrine, EngineInterface $templateEngine, Reader $reader) {
$this->templateEngine = $templateEngine;
$this->em = $doctrine->getManager();
+ $this->reader = $reader;
}
/**
@@ -38,27 +42,36 @@ class ReflectionService {
public function getEntity($entity)
{
- $entity = $this->convertExtJSToPHPClassName($entity);
+ $entity = $this->convertExtJSToPHPClassName($entity);
+
+ $cm = $this->em->getClassMetadata($entity);
- $cm = $this->em->getClassMetadata($entity);
+ $fields = $cm->getFieldNames();
- $fields = $cm->getFieldNames();
+ $mappings = array();
- $mappings = array();
+ foreach ($fields as $field) {
+ $currentMapping = $cm->getFieldMapping($field);
- foreach ($fields as $field) {
- $currentMapping = $cm->getFieldMapping($field);
+ $mappings[] = array(
+ "name" => $currentMapping["fieldName"],
+ "type" => $this->getExtJSFieldMapping($currentMapping["type"]),
+ );
+ }
- $mappings[] = array(
- "name" => $currentMapping["fieldName"],
- "type" => $this->getExtJSFieldMapping($currentMapping["type"]),
+ $renderParams = array(
+ "fields" => $mappings,
+ "className" => $this->convertPHPToExtJSClassName($entity),
);
- }
- $renderParams = array(
- "fields" => $mappings,
- "className" => $this->convertPHPToExtJSClassName($entity),
- );
+ $targetService = $this->reader->getClassAnnotation(
+ $cm->getReflectionClass(),
+ "PartKeepr\DoctrineReflectionBundle\Annotation\TargetService"
+ );
+
+ if ($targetService !== null) {
+ $renderParams["uri"] = $targetService->uri;
+ }
return $this->templateEngine->render('PartKeeprDoctrineReflectionBundle::model.js.twig', $renderParams);
}
diff --git a/src/PartKeepr/SiPrefixBundle/Entity/SiPrefix.php b/src/PartKeepr/SiPrefixBundle/Entity/SiPrefix.php
@@ -3,7 +3,8 @@ namespace PartKeepr\SiPrefixBundle\Entity;
use PartKeepr\Util\BaseEntity,
Doctrine\ORM\Mapping as ORM,
- Symfony\Component\Validator\Constraints as Assert;
+ Symfony\Component\Validator\Constraints as Assert,
+ PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
/**
* Represents an SI Prefix
@@ -11,6 +12,7 @@ use PartKeepr\Util\BaseEntity,
* @link http://en.wikipedia.org/wiki/Metric_prefix
*
* @ORM\Entity
+ * @TargetService(uri="/siprefix")
*/
class SiPrefix extends BaseEntity
{