From 150dc50003f9e37c92bdc148a3477c18213d4364 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 21 Jan 2020 12:55:03 +0100 Subject: [PATCH] :zap: return arrays as such --- .env | 4 ++ .htaccess | 1 + composer.json | 5 ++- composer.lock | 64 +++++++++++++++++++++++++++- config/bundles.php | 1 + config/packages/nelmio_cors.yaml | 15 +++++++ config/packages/nelmio_cors.yaml~ | 15 +++++++ src/Controller/DefaultController.php | 6 +-- src/Entity/StackOfVotes.php | 6 +-- symfony.lock | 12 ++++++ 10 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 .htaccess create mode 100644 config/packages/nelmio_cors.yaml create mode 100644 config/packages/nelmio_cors.yaml~ diff --git a/.env b/.env index b914756..22a902b 100644 --- a/.env +++ b/.env @@ -27,3 +27,7 @@ APP_SECRET=597b0529ac702d27dcb9089f7e69c362 # IMPORTANT: You MUST also configure your db driver and server_version in config/packages/doctrine.yaml DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name ###< doctrine/doctrine-bundle ### + +###> nelmio/cors-bundle ### +CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$ +###< nelmio/cors-bundle ### diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..f289550 --- /dev/null +++ b/.htaccess @@ -0,0 +1 @@ +Header set Access-Control-Allow-Origin "*" diff --git a/composer.json b/composer.json index 4f7b111..cc1aa09 100644 --- a/composer.json +++ b/composer.json @@ -5,9 +5,11 @@ "php": "^7.1.3", "ext-ctype": "*", "ext-iconv": "*", + "ext-json": "*", "friendsofsymfony/rest-bundle": "^2.6", "jms/serializer-bundle": "^3.4", "nelmio/api-doc-bundle": "^3.4", + "nelmio/cors-bundle": "^2.0", "sensio/framework-extra-bundle": "^5.5", "symfony/console": "4.3.*", "symfony/dotenv": "4.3.*", @@ -18,8 +20,7 @@ "symfony/maker-bundle": "^1.14", "symfony/orm-pack": "^1.0", "symfony/validator": "4.3.*", - "symfony/yaml": "4.3.*", - "ext-json": "*" + "symfony/yaml": "4.3.*" }, "require-dev": { "doctrine/doctrine-fixtures-bundle": "^3.2", diff --git a/composer.lock b/composer.lock index dd25dd7..f4dcf12 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": "fa70b5946a14ecedaa82c3754b1601e7", + "content-hash": "41bf5f0471bb141a097830cb05bbf807", "packages": [ { "name": "doctrine/annotations", @@ -2489,6 +2489,63 @@ ], "time": "2019-02-18T16:27:10+00:00" }, + { + "name": "nelmio/cors-bundle", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/nelmio/NelmioCorsBundle.git", + "reference": "9683e6d30d000ef998919261329d825de7c53499" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nelmio/NelmioCorsBundle/zipball/9683e6d30d000ef998919261329d825de7c53499", + "reference": "9683e6d30d000ef998919261329d825de7c53499", + "shasum": "" + }, + "require": { + "symfony/framework-bundle": "^4.3 || ^5.0" + }, + "require-dev": { + "mockery/mockery": "^1.2", + "symfony/phpunit-bridge": "^4.3 || ^5.0" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Nelmio\\CorsBundle\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nelmio", + "homepage": "http://nelm.io" + }, + { + "name": "Symfony Community", + "homepage": "https://github.com/nelmio/NelmioCorsBundle/contributors" + } + ], + "description": "Adds CORS (Cross-Origin Resource Sharing) headers support in your Symfony application", + "keywords": [ + "api", + "cors", + "crossdomain" + ], + "time": "2019-11-15T08:54:08+00:00" + }, { "name": "nikic/php-parser", "version": "v4.2.5", @@ -5707,6 +5764,7 @@ "code", "zf" ], + "abandoned": "laminas/laminas-code", "time": "2019-10-05T23:18:22+00:00" }, { @@ -5761,6 +5819,7 @@ "events", "zf2" ], + "abandoned": "laminas/laminas-eventmanager", "time": "2018-04-25T15:33:34+00:00" }, { @@ -6068,7 +6127,8 @@ "platform": { "php": "^7.1.3", "ext-ctype": "*", - "ext-iconv": "*" + "ext-iconv": "*", + "ext-json": "*" }, "platform-dev": [] } diff --git a/config/bundles.php b/config/bundles.php index 943d432..4c68d90 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -12,4 +12,5 @@ return [ Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true], Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true], + Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true], ]; diff --git a/config/packages/nelmio_cors.yaml b/config/packages/nelmio_cors.yaml new file mode 100644 index 0000000..b18bd62 --- /dev/null +++ b/config/packages/nelmio_cors.yaml @@ -0,0 +1,15 @@ +nelmio_cors: + defaults: + origin_regex: true + allow_origin: ['%env(CORS_ALLOW_ORIGIN)%'] + allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'] + allow_headers: ['Content-Type', 'Authorization'] + expose_headers: ['Link'] + max_age: 3600 + paths: + '^/': null + '^/api/': + allow_origin: ['*'] + allow_headers: ['Content-Type', 'Authorization'] + allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'] + max_age: 3600 diff --git a/config/packages/nelmio_cors.yaml~ b/config/packages/nelmio_cors.yaml~ new file mode 100644 index 0000000..d49969b --- /dev/null +++ b/config/packages/nelmio_cors.yaml~ @@ -0,0 +1,15 @@ +nelmio_cors: + defaults: + origin_regex: true + allow_origin: ['%env(CORS_ALLOW_ORIGIN)%'] + allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'] + allow_headers: ['Content-Type', 'Authorization'] + expose_headers: ['Link'] + max_age: 3600 + paths: + '^/': null + '^/api/': + allow_origin: ['*'] + allow_headers: ['X-Custom-Auth'] + allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'] + max_age: 3600 diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 93d9414..37435af 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -226,13 +226,13 @@ class DefaultController extends AbstractController { $stacks = []; $choices = []; foreach ( $poll->getComments() as $c ) { - $comments[ $c->getId() ] = $c->display(); + $comments[] = $c->display(); } foreach ( $poll->getStacksOfVotes() as $c ) { - $stacks[ $c->getId() ] = $c->display(); + $stacks[] = $c->display(); } foreach ( $poll->getChoices() as $c ) { - $choices[ $c->getId() ] = $c->display(); + $choices[] = $c->display(); } $returnedPoll = [ 'message' => 'your poll config', diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 67d56d3..8b7707f 100644 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -50,15 +50,15 @@ class StackOfVotes { 'votes' => [], ]; foreach ( $this->getVotes() as $vote ) { - $tab[ 'votes' ][ $vote->getId() ] = [ + $tab[ 'votes' ][] = [ 'id' => $this->getId(), 'vote_id' => $vote->getId(), 'value' => $vote->getValue(), 'choice_id' => $vote->getChoice()->getId(), 'text' => $vote->getChoice()->getName(), ]; - $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); - $tab[ 'creation_date' ] = $vote->getCreationDate(); + $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); + $tab[ 'creation_date' ] = $vote->getCreationDate(); } return $tab; diff --git a/symfony.lock b/symfony.lock index ecf99f5..f0adf7f 100644 --- a/symfony.lock +++ b/symfony.lock @@ -183,6 +183,18 @@ "config/routes/nelmio_api_doc.yaml" ] }, + "nelmio/cors-bundle": { + "version": "1.5", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "1.5", + "ref": "6388de23860284db9acce0a7a5d9d13153bcb571" + }, + "files": [ + "config/packages/nelmio_cors.yaml" + ] + }, "nikic/php-parser": { "version": "v4.2.4" },