partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 7438b71773ab12259c7dec445c14e334a2f671ec
parent e8be4c5e641748802b99a12714dc798a37453321
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 12 Sep 2015 13:56:56 +0200

Replaced format listener, temporarily removed NelmioApiDocBundle

Diffstat:
Mapp/AppKernel.php | 1-
Asrc/PartKeepr/CategoryBundle/Action/GetRootNodeAction.php | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/CategoryBundle/Controller/CategoryController.php | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/PartKeepr/PartBundle/Controller/PartController.php | 2+-
Msrc/PartKeepr/PartBundle/Controller/PartMeasurementUnitController.php | 2+-
Msrc/PartKeepr/UploadedFileBundle/Controller/FileController.php | 2+-
6 files changed, 136 insertions(+), 6 deletions(-)

diff --git a/app/AppKernel.php b/app/AppKernel.php @@ -60,7 +60,6 @@ class AppKernel extends Kernel new FOS\RestBundle\FOSRestBundle(), new FOS\UserBundle\FOSUserBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), - new Nelmio\ApiDocBundle\NelmioApiDocBundle(), new \Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), new PartKeepr\DoctrineReflectionBundle\PartKeeprDoctrineReflectionBundle(), new PartKeepr\RESTBundle\PartKeeprRESTBundle(), diff --git a/src/PartKeepr/CategoryBundle/Action/GetRootNodeAction.php b/src/PartKeepr/CategoryBundle/Action/GetRootNodeAction.php @@ -0,0 +1,77 @@ +<?php +/* + * This file is part of the DunglasApiBundle package. + * + * (c) Kévin Dunglas <dunglas@gmail.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Dunglas\ApiBundle\Action; +use Dunglas\ApiBundle\Api\ResourceInterface; +use Dunglas\ApiBundle\Exception\RuntimeException; +use Dunglas\ApiBundle\Model\DataProviderInterface; +use Gedmo\Tree\Entity\Repository\AbstractTreeRepository; +use Symfony\Bridge\Doctrine\ManagerRegistry; +use Symfony\Component\HttpFoundation\Request; +/** + * Default API action retrieving a collection of resources. + * + * @author Kévin Dunglas <dunglas@gmail.com> + */ +class GetRootNodeAction +{ + use ActionUtilTrait; + /** + * @var DataProviderInterface + */ + private $dataProvider; + + /** + * @var ManagerRegistry + */ + private $manager; + + public function __construct(DataProviderInterface $dataProvider, ManagerRegistry $manager) + { + $this->dataProvider = $dataProvider; + $this->manager = $manager; + } + /** + * Retrieves a collection of resources. + * + * @param Request $request + * + * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable + * + * @throws RuntimeException + */ + public function __invoke(Request $request) + { + list($resourceType) = $this->extractAttributes($request); + //return $this->dataProvider->getCollection($resourceType); + + /** + * @var ResourceInterface $resourceType + */ + $repository = $this->manager->getRepository($resourceType->getEntityClass()); + + /** + * @var $repository AbstractTreeRepository + */ + $rootNode = $repository->getRootNodes()[0]; + + return $rootNode; + /*$data = $this->get('serializer')->normalize( + $rootNode, + 'json-ld', + $resource->getNormalizationContext() + ); + + $responseData = array("children" => $data); + + return new JsonResponse( + $responseData + );*/ + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/CategoryBundle/Controller/CategoryController.php b/src/PartKeepr/CategoryBundle/Controller/CategoryController.php @@ -1,18 +1,67 @@ <?php namespace PartKeepr\CategoryBundle\Controller; -use Dunglas\ApiBundle\Controller\ResourceController; +use Dunglas\ApiBundle\Api\ResourceInterface; use FOS\RestBundle\Controller\Annotations\RequestParam; use Gedmo\Tree\Entity\Repository\AbstractTreeRepository; use PartKeepr\CategoryBundle\Exception\MissingParentCategoryException; use PartKeepr\CategoryBundle\Exception\RootMayNotBeMovedException; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -class CategoryController extends ResourceController +class CategoryController extends Controller { /** + * @var ResourceInterface + */ + private $resource; + /** + * Gets the Resource associated with the current Request. + * Must be called before manipulating the resource. + * + * @param Request $request + * + * @return ResourceInterface + * + * @throws InvalidArgumentException + */ + protected function getResource(Request $request) + { + if ($this->resource) { + return $this->resource; + } + if (!$request->attributes->has('_resource')) { + throw new InvalidArgumentException('The current request doesn\'t have an associated resource.'); + } + $shortName = $request->attributes->get('_resource'); + if (!($this->resource = $this->get('api.resource_collection')->getResourceForShortName($shortName))) { + throw new InvalidArgumentException(sprintf('The resource "%s" cannot be found.', $shortName)); + } + return $this->resource; + } + + /** + * Finds an object of throws a 404 error. + * + * @param ResourceInterface $resource + * @param string|int $id + * + * @return object + * + * @throws NotFoundHttpException + */ + protected function findOrThrowNotFound(ResourceInterface $resource, $id) + { + $item = $this->get('api.data_provider')->getItem($resource, $id, true); + if (!$item) { + throw $this->createNotFoundException(); + } + return $item; + } + + /** * Moves a node to another node * * @RequestParam(name="parent",description="The ID of the new parent") @@ -69,6 +118,10 @@ class CategoryController extends ResourceController */ $rootNode = $repository->getRootNodes()[0]; + //$this->get('serializer')->setCircularReferenceLimit(999); + //$normalizer = $this->get('serializer')->getNormalizer($rootNode, 'json-ld'); + //$normalizer->setCircularReferenceLimit(999); + $data = $this->get('serializer')->normalize( $rootNode, 'json-ld', diff --git a/src/PartKeepr/PartBundle/Controller/PartController.php b/src/PartKeepr/PartBundle/Controller/PartController.php @@ -1,9 +1,9 @@ <?php namespace PartKeepr\PartBundle\Controller; -use Dunglas\ApiBundle\Controller\ResourceController; use Dunglas\ApiBundle\JsonLd\Response; use FOS\RestBundle\Controller\Annotations\RequestParam; +use Nelmio\ApiDocBundle\Tests\Fixtures\Controller\ResourceController; use PartKeepr\PartBundle\Entity\Part; use PartKeepr\Stock\StockEntry; use Symfony\Component\HttpFoundation\Request; diff --git a/src/PartKeepr/PartBundle/Controller/PartMeasurementUnitController.php b/src/PartKeepr/PartBundle/Controller/PartMeasurementUnitController.php @@ -2,8 +2,8 @@ namespace PartKeepr\PartBundle\Controller; use Doctrine\ORM\EntityManager; -use Dunglas\ApiBundle\Controller\ResourceController; use Dunglas\ApiBundle\JsonLd\Response; +use Nelmio\ApiDocBundle\Tests\Fixtures\Controller\ResourceController; use Symfony\Component\HttpFoundation\Request; class PartMeasurementUnitController extends ResourceController diff --git a/src/PartKeepr/UploadedFileBundle/Controller/FileController.php b/src/PartKeepr/UploadedFileBundle/Controller/FileController.php @@ -2,7 +2,7 @@ namespace PartKeepr\UploadedFileBundle\Controller; use Doctrine\ORM\EntityManager; -use Dunglas\ApiBundle\Controller\ResourceController; +use Nelmio\ApiDocBundle\Tests\Fixtures\Controller\ResourceController; use PartKeepr\UploadedFileBundle\Entity\UploadedFile; use Symfony\Component\HttpFoundation\Response;