From ea7db84cb60b1a6ad141894849e8f9ad5b982e84 Mon Sep 17 00:00:00 2001 From: Aymeric Ratinaud Date: Sat, 3 Jul 2021 19:06:53 +0200 Subject: [PATCH] custom controller action from database using Paginator --- README.md | 11 + composer.json | 1 + composer.lock | 403 +++++++++++++++++++++- config/bundles.php | 3 + config/packages/dev/hautelook_alice.yaml | 2 + config/packages/dev/nelmio_alice.yaml | 9 + config/packages/test/hautelook_alice.yaml | 2 + config/packages/test/nelmio_alice.yaml | 2 + fixtures/.gitignore | 0 fixtures/movie.yaml | 3 + src/Controller/CustomMovieAction.php | 31 ++ src/Entity/Movie.php | 54 +++ src/Repository/MovieRepository.php | 52 +++ symfony.lock | 42 +++ 14 files changed, 614 insertions(+), 1 deletion(-) create mode 100644 config/packages/dev/hautelook_alice.yaml create mode 100644 config/packages/dev/nelmio_alice.yaml create mode 100644 config/packages/test/hautelook_alice.yaml create mode 100644 config/packages/test/nelmio_alice.yaml create mode 100644 fixtures/.gitignore create mode 100644 fixtures/movie.yaml create mode 100644 src/Controller/CustomMovieAction.php create mode 100644 src/Entity/Movie.php create mode 100644 src/Repository/MovieRepository.php diff --git a/README.md b/README.md index 90575e7..f10a98f 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,25 @@ CityFilter pass the `sort` and `order` params to the context and this context it composer install + php bin/console doctrine:schema:update --env=dev --dump-sql + + php bin/console doctrine:schema:update --env=dev --force + + php bin/console hautelook:fixtures:load + ## Launch symfony serve --no-tls ## Usage +sort and filter on raw data from csv + `/api/cities?search[key]=tokyo&order[key]=desc&page=1` +custom controller action from database using Paginator +`api/movies/custom-action?page=1&order[id]=asc&order[title]=desc` + ### Keys available - id diff --git a/composer.json b/composer.json index a478ed6..6859ccc 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "twig/twig": "^2.12|^3.0" }, "require-dev": { + "hautelook/alice-bundle": "^2.9", "phpunit/phpunit": "^9.5", "symfony/browser-kit": "^5.3", "symfony/css-selector": "^5.3", diff --git a/composer.lock b/composer.lock index 659a17b..0187b30 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "95a422740abef9e84162fbb3a587ee10", + "content-hash": "19f4cc758a8d041490178cc4cbc9639f", "packages": [ { "name": "api-platform/core", @@ -7999,6 +7999,226 @@ } ], "packages-dev": [ + { + "name": "doctrine/data-fixtures", + "version": "1.5.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/data-fixtures.git", + "reference": "51d3d4880d28951fff42a635a2389f8c63baddc5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/51d3d4880d28951fff42a635a2389f8c63baddc5", + "reference": "51d3d4880d28951fff42a635a2389f8c63baddc5", + "shasum": "" + }, + "require": { + "doctrine/common": "^2.13|^3.0", + "doctrine/persistence": "^1.3.3|^2.0", + "php": "^7.2 || ^8.0" + }, + "conflict": { + "doctrine/phpcr-odm": "<1.3.0" + }, + "require-dev": { + "doctrine/coding-standard": "^8.2", + "doctrine/dbal": "^2.5.4", + "doctrine/mongodb-odm": "^1.3.0 || ^2.0.0", + "doctrine/orm": "^2.7.0", + "ext-sqlite3": "*", + "phpunit/phpunit": "^8.0" + }, + "suggest": { + "alcaeus/mongo-php-adapter": "For using MongoDB ODM 1.3 with PHP 7 (deprecated)", + "doctrine/mongodb-odm": "For loading MongoDB ODM fixtures", + "doctrine/orm": "For loading ORM fixtures", + "doctrine/phpcr-odm": "For loading PHPCR ODM fixtures" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\DataFixtures\\": "lib/Doctrine/Common/DataFixtures" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Data Fixtures for all Doctrine Object Managers", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "database" + ], + "support": { + "issues": "https://github.com/doctrine/data-fixtures/issues", + "source": "https://github.com/doctrine/data-fixtures/tree/1.5.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdata-fixtures", + "type": "tidelift" + } + ], + "time": "2021-01-23T10:20:43+00:00" + }, + { + "name": "fakerphp/faker", + "version": "v1.14.1", + "source": { + "type": "git", + "url": "https://github.com/FakerPHP/Faker.git", + "reference": "ed22aee8d17c7b396f74a58b1e7fefa4f90d5ef1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FakerPHP/Faker/zipball/ed22aee8d17c7b396f74a58b1e7fefa4f90d5ef1", + "reference": "ed22aee8d17c7b396f74a58b1e7fefa4f90d5ef1", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0", + "psr/container": "^1.0", + "symfony/deprecation-contracts": "^2.2" + }, + "conflict": { + "fzaninotto/faker": "*" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-intl": "*", + "symfony/phpunit-bridge": "^4.4 || ^5.2" + }, + "suggest": { + "ext-curl": "Required by Faker\\Provider\\Image to download images.", + "ext-dom": "Required by Faker\\Provider\\HtmlLorem for generating random HTML.", + "ext-iconv": "Required by Faker\\Provider\\ru_RU\\Text::realText() for generating real Russian text.", + "ext-mbstring": "Required for multibyte Unicode string functionality." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "v1.15-dev" + } + }, + "autoload": { + "psr-4": { + "Faker\\": "src/Faker/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "François Zaninotto" + } + ], + "description": "Faker is a PHP library that generates fake data for you.", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "support": { + "issues": "https://github.com/FakerPHP/Faker/issues", + "source": "https://github.com/FakerPHP/Faker/tree/v.1.14.1" + }, + "time": "2021-03-30T06:27:33+00:00" + }, + { + "name": "hautelook/alice-bundle", + "version": "2.9.0", + "source": { + "type": "git", + "url": "https://github.com/hautelook/AliceBundle.git", + "reference": "17c5199b2a6efbc1383b0afe1cddfa3c176b7b6f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/hautelook/AliceBundle/zipball/17c5199b2a6efbc1383b0afe1cddfa3c176b7b6f", + "reference": "17c5199b2a6efbc1383b0afe1cddfa3c176b7b6f", + "shasum": "" + }, + "require": { + "doctrine/data-fixtures": "^1.2", + "doctrine/doctrine-bundle": "^1.8 || ^2.0", + "doctrine/orm": "^2.5.11", + "doctrine/persistence": "^1.3.4 || ^2.0", + "php": "^7.3 || ^8.0", + "psr/log": "^1.0", + "symfony/finder": "^3.4 || ^4.0 || ^5.0", + "symfony/framework-bundle": "^3.4.24 || ^4.0 || ^5.0", + "theofidry/alice-data-fixtures": "^1.4" + }, + "require-dev": { + "phpspec/prophecy": "^1.7", + "phpunit/phpunit": "^8.5", + "symfony/phpunit-bridge": "^5.1" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + }, + "bin-dir": "bin", + "sort-packages": true + }, + "autoload": { + "psr-4": { + "Hautelook\\AliceBundle\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Baldur Rensch", + "email": "brensch@gmail.com" + }, + { + "name": "Théo FIDRY", + "email": "theo.fidry@gmail.com", + "homepage": "https://github.com/theofidry" + } + ], + "description": "Symfony bundle to manage fixtures with Alice and Faker.", + "keywords": [ + "Fixture", + "alice", + "faker", + "orm", + "symfony" + ], + "support": { + "issues": "https://github.com/hautelook/AliceBundle/issues", + "source": "https://github.com/hautelook/AliceBundle/tree/2.9.0" + }, + "funding": [ + { + "url": "https://github.com/theofidry", + "type": "github" + } + ], + "time": "2021-02-23T08:45:57+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.10.2", @@ -8057,6 +8277,96 @@ ], "time": "2020-11-13T09:40:50+00:00" }, + { + "name": "nelmio/alice", + "version": "v3.8.2", + "source": { + "type": "git", + "url": "https://github.com/nelmio/alice.git", + "reference": "b57f4c6f039baa72e2bd5b75fd8de77e1b9228e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nelmio/alice/zipball/b57f4c6f039baa72e2bd5b75fd8de77e1b9228e3", + "reference": "b57f4c6f039baa72e2bd5b75fd8de77e1b9228e3", + "shasum": "" + }, + "require": { + "fakerphp/faker": "^1.10", + "myclabs/deep-copy": "^1.10", + "php": "^7.3 || ^8.0", + "sebastian/comparator": "^3.0 || ^4.0", + "symfony/property-access": "^2.8 || ^3.4 || ^4.0 || ^5.0", + "symfony/yaml": "^2.8 || ^3.4 || ^4.0 || ^5.0" + }, + "conflict": { + "symfony/framework-bundle": "<3.4" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.1.0", + "phpspec/prophecy": "^1.6", + "phpspec/prophecy-phpunit": "^2.0", + "phpunit/phpunit": "^8.5.4 || ^9.3", + "symfony/phpunit-bridge": "^5.1.3", + "symfony/var-dumper": "^3.4 || ^4.0 || ^5.0" + }, + "suggest": { + "theofidry/alice-data-fixtures": "Wrapper for Alice to provide a persistence layer." + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": false + }, + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "files": [ + "src/deep_clone.php" + ], + "psr-4": { + "Nelmio\\Alice\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be" + }, + { + "name": "Tim Shelburne", + "email": "shelburt02@gmail.com" + }, + { + "name": "Théo FIDRY", + "email": "theo.fidry@gmail.com" + } + ], + "description": "Expressive fixtures generator", + "keywords": [ + "Fixture", + "data", + "faker", + "test" + ], + "support": { + "issues": "https://github.com/nelmio/alice/issues", + "source": "https://github.com/nelmio/alice/tree/v3.8.2" + }, + "funding": [ + { + "url": "https://github.com/theofidry", + "type": "github" + } + ], + "time": "2021-02-27T19:13:09+00:00" + }, { "name": "nikic/php-parser", "version": "v4.10.5", @@ -10214,6 +10524,97 @@ ], "time": "2021-06-07T14:51:59+00:00" }, + { + "name": "theofidry/alice-data-fixtures", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/theofidry/AliceDataFixtures.git", + "reference": "bcfdf64bc940eb4a7b40b46d9ca5251e5692cc11" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theofidry/AliceDataFixtures/zipball/bcfdf64bc940eb4a7b40b46d9ca5251e5692cc11", + "reference": "bcfdf64bc940eb4a7b40b46d9ca5251e5692cc11", + "shasum": "" + }, + "require": { + "nelmio/alice": "^3.5", + "php": "^7.3 | ^8.0", + "psr/log": "^1.0" + }, + "conflict": { + "doctrine/orm": "<2.6.3", + "illuminate/database": "<5.5", + "ocramius/proxy-manager": "<2.1", + "symfony/framework-bundle": "<3.4", + "zendframework/zend-code": "<3.3.1" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.1", + "phpspec/prophecy": "^1.6", + "phpspec/prophecy-phpunit": "^2.0", + "phpunit/phpunit": "^9.3", + "symfony/phpunit-bridge": "^5.1.3" + }, + "suggest": { + "alcaeus/mongo-php-adapter": "To use Doctrine with the MongoDB flavour", + "doctrine/data-fixtures": "To use Doctrine", + "doctrine/dbal": "To use Doctrine with the PHPCR flavour", + "doctrine/mongodb": "To use Doctrine with the MongoDB flavour", + "doctrine/mongodb-odm": "To use Doctrine with the MongoDB flavour", + "doctrine/orm": "To use Doctrine ORM", + "doctrine/phpcr-odm": "To use Doctrine with the PHPCR flavour", + "illuminate/database": "To use Eloquent", + "jackalope/jackalope-doctrine-dbal": "To use Doctrine with the PHPCR flavour", + "ocramius/proxy-manager": "To avoid database connection on kernel boot" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": false + }, + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Fidry\\AliceDataFixtures\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Théo FIDRY", + "email": "theo.fidry@gmail.com", + "homepage": "https://github.com/theofidry" + } + ], + "description": "Nelmio alice extension to persist the loaded fixtures.", + "keywords": [ + "Fixture", + "alice", + "data", + "faker", + "orm", + "tests" + ], + "support": { + "issues": "https://github.com/theofidry/AliceDataFixtures/issues", + "source": "https://github.com/theofidry/AliceDataFixtures/tree/1.4.0" + }, + "funding": [ + { + "url": "https://github.com/theofidry", + "type": "github" + } + ], + "time": "2021-01-25T10:07:17+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.0", diff --git a/config/bundles.php b/config/bundles.php index 3feb9a9..c662635 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -14,4 +14,7 @@ Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true], Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true], ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true], + Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true], + Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true], + Hautelook\AliceBundle\HautelookAliceBundle::class => ['dev' => true, 'test' => true], ]; diff --git a/config/packages/dev/hautelook_alice.yaml b/config/packages/dev/hautelook_alice.yaml new file mode 100644 index 0000000..b685db1 --- /dev/null +++ b/config/packages/dev/hautelook_alice.yaml @@ -0,0 +1,2 @@ +hautelook_alice: + fixtures_path: fixtures diff --git a/config/packages/dev/nelmio_alice.yaml b/config/packages/dev/nelmio_alice.yaml new file mode 100644 index 0000000..4cb9065 --- /dev/null +++ b/config/packages/dev/nelmio_alice.yaml @@ -0,0 +1,9 @@ +nelmio_alice: + functions_blacklist: + - 'current' + - 'shuffle' + - 'date' + - 'time' + - 'file' + - 'md5' + - 'sha1' diff --git a/config/packages/test/hautelook_alice.yaml b/config/packages/test/hautelook_alice.yaml new file mode 100644 index 0000000..7b71774 --- /dev/null +++ b/config/packages/test/hautelook_alice.yaml @@ -0,0 +1,2 @@ +imports: + - { resource: ../dev/hautelook_alice.yaml } diff --git a/config/packages/test/nelmio_alice.yaml b/config/packages/test/nelmio_alice.yaml new file mode 100644 index 0000000..caec543 --- /dev/null +++ b/config/packages/test/nelmio_alice.yaml @@ -0,0 +1,2 @@ +imports: + - { resource: ../dev/nelmio_alice.yaml } diff --git a/fixtures/.gitignore b/fixtures/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/movie.yaml b/fixtures/movie.yaml new file mode 100644 index 0000000..d85e1ea --- /dev/null +++ b/fixtures/movie.yaml @@ -0,0 +1,3 @@ +App\Entity\Movie: + movie_{1..1000}: + title: diff --git a/src/Controller/CustomMovieAction.php b/src/Controller/CustomMovieAction.php new file mode 100644 index 0000000..819e609 --- /dev/null +++ b/src/Controller/CustomMovieAction.php @@ -0,0 +1,31 @@ +movieRepository = $movieRepository; + } + + public function __invoke(Request $request): Paginator + { + $page = (int) $request->query->get('page', 1); + $order = $request->query->get('order', []); + + return $this->movieRepository->getCustom($page, $order); + } +} diff --git a/src/Entity/Movie.php b/src/Entity/Movie.php new file mode 100644 index 0000000..f805032 --- /dev/null +++ b/src/Entity/Movie.php @@ -0,0 +1,54 @@ +id; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } +} diff --git a/src/Repository/MovieRepository.php b/src/Repository/MovieRepository.php new file mode 100644 index 0000000..8a1ee96 --- /dev/null +++ b/src/Repository/MovieRepository.php @@ -0,0 +1,52 @@ +createQueryBuilder('b'); + + if (false === empty($orders)) { + foreach ($orders as $sort => $order) { + $queryBuilder->addOrderBy('b.'.$sort, $order); + } + } + + $criteria = Criteria::create() + ->setFirstResult($firstResult) + ->setMaxResults(self::ITEMS_PER_PAGE) + ; + $queryBuilder->addCriteria($criteria); + + $doctrinePaginator = new DoctrinePaginator($queryBuilder); + $paginator = new Paginator($doctrinePaginator); + + return $paginator; + } + +} diff --git a/symfony.lock b/symfony.lock index afda607..8e64c26 100644 --- a/symfony.lock +++ b/symfony.lock @@ -40,6 +40,9 @@ "doctrine/common": { "version": "3.1.2" }, + "doctrine/data-fixtures": { + "version": "1.5.0" + }, "doctrine/dbal": { "version": "2.13.2" }, @@ -102,12 +105,29 @@ "egulias/email-validator": { "version": "3.1.1" }, + "fakerphp/faker": { + "version": "v1.14.1" + }, "fig/link-util": { "version": "1.1.2" }, "friendsofphp/proxy-manager-lts": { "version": "v1.0.5" }, + "hautelook/alice-bundle": { + "version": "2.1", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "2.1", + "ref": "71822522faf7ed2792d86b7f94ce73443358ccb9" + }, + "files": [ + "config/packages/dev/hautelook_alice.yaml", + "config/packages/test/hautelook_alice.yaml", + "fixtures/.gitignore" + ] + }, "laminas/laminas-code": { "version": "4.4.0" }, @@ -117,6 +137,19 @@ "myclabs/deep-copy": { "version": "1.10.2" }, + "nelmio/alice": { + "version": "3.2", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "3.2", + "ref": "0b9900ece737bec7752e4155c0164639dd9b0cb0" + }, + "files": [ + "config/packages/dev/nelmio_alice.yaml", + "config/packages/test/nelmio_alice.yaml" + ] + }, "nelmio/cors-bundle": { "version": "1.5", "recipe": { @@ -630,6 +663,15 @@ "symfony/yaml": { "version": "v5.3.3" }, + "theofidry/alice-data-fixtures": { + "version": "1.0", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "1.0", + "ref": "fe5a50faf580eb58f08ada2abe8afbd2d4941e05" + } + }, "theseer/tokenizer": { "version": "1.2.0" },