Skip to content

Commit 7498198

Browse files
committed
Initial Commit
0 parents  commit 7498198

16 files changed

+598
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
use SymfonyBundles\JsonRequestBundle\EventListener\RequestTransformerListener;
8+
9+
class Configuration implements ConfigurationInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function getConfigTreeBuilder()
15+
{
16+
$builder = new TreeBuilder('sb_json_request');
17+
18+
if (\method_exists($builder, 'getRootNode')) {
19+
$rootNode = $builder->getRootNode();
20+
} else {
21+
// BC layer for symfony/config 4.1 and older
22+
$rootNode = $builder->root('maker');
23+
}
24+
25+
$rootNode
26+
->children()
27+
->arrayNode('listener')
28+
->addDefaultsIfNotSet()
29+
->children()
30+
->scalarNode('request_transformer')
31+
->defaultValue(RequestTransformerListener::class)
32+
->end()
33+
->scalarNode('priority')
34+
->defaultValue(100)
35+
->end()
36+
->end()
37+
->end()
38+
->end();
39+
40+
return $builder;
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\Definition;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
8+
9+
class JsonRequestExtension extends ConfigurableExtension
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected function loadInternal(array $configs, ContainerBuilder $container)
15+
{
16+
$listener = new Definition($configs['listener']['request_transformer']);
17+
18+
$listener->addTag('kernel.event_listener', [
19+
'event' => 'kernel.request',
20+
'method' => 'onKernelRequest',
21+
'priority' => $configs['listener']['priority'],
22+
]);
23+
24+
$container->setDefinition('sb_json_request.request_transformer', $listener);
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function getAlias()
31+
{
32+
return 'sb_json_request';
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle\EventListener;
4+
5+
use Symfony\Component\HttpKernel\Event\RequestEvent;
6+
7+
interface RequestListenerInterface
8+
{
9+
/**
10+
* @param RequestEvent $event
11+
*/
12+
public function onKernelRequest(RequestEvent $event): void;
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle\EventListener;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpKernel\Event\RequestEvent;
8+
9+
class RequestTransformerListener implements RequestListenerInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function onKernelRequest(RequestEvent $event): void
15+
{
16+
$request = $event->getRequest();
17+
18+
if (false === $this->isAvailable($request)) {
19+
return;
20+
}
21+
22+
if (false === $this->transform($request)) {
23+
$response = new Response('Unable to parse request.', Response::HTTP_BAD_REQUEST);
24+
25+
$event->setResponse($response);
26+
}
27+
}
28+
29+
/**
30+
* @param Request $request
31+
*
32+
* @return bool
33+
*/
34+
private function isAvailable(Request $request): bool
35+
{
36+
return 'json' === $request->getContentType() && $request->getContent();
37+
}
38+
39+
/**
40+
* @param Request $request
41+
*
42+
* @return bool
43+
*/
44+
private function transform(Request $request): bool
45+
{
46+
$data = \json_decode($request->getContent(), true);
47+
48+
if (\json_last_error() !== \JSON_ERROR_NONE) {
49+
return false;
50+
}
51+
52+
if (\is_array($data)) {
53+
$request->request->replace($data);
54+
}
55+
56+
return true;
57+
}
58+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016-2017 Symfony Bundles (Dmitry Khaperets)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
SymfonyBundles JsonRequest Bundle
2+
=================================
3+
4+
[![SensioLabsInsight][sensiolabs-insight-image]][sensiolabs-insight-link]
5+
6+
[![Build Status][testing-image]][testing-link]
7+
[![Scrutinizer Code Quality][scrutinizer-code-quality-image]][scrutinizer-code-quality-link]
8+
[![Code Coverage][code-coverage-image]][code-coverage-link]
9+
[![Total Downloads][downloads-image]][package-link]
10+
[![Latest Stable Version][stable-image]][package-link]
11+
[![License][license-image]][license-link]
12+
13+
What is JsonRequest Bundle?
14+
---------------------------
15+
This bundle will help you to work with json requests as standard requests without using «crutches».
16+
If previously for fetching of data from the request you did like this:
17+
`$data = json_decode($request->getContent())`,
18+
it is now in this already there is no need to.
19+
20+
For example when sending json-request from AngularJS, Vue.js or etc.
21+
Early:
22+
``` php
23+
public function indexAction(Request $request)
24+
{
25+
$data = json_decode($request->getContent(), true);
26+
27+
// uses request data
28+
$name = isset($data['name']) ? $data['name'] : null;
29+
}
30+
```
31+
32+
Now you can work with json-request as with standard request:
33+
``` php
34+
public function indexAction(Request $request)
35+
{
36+
$name = $request->get('name');
37+
}
38+
```
39+
40+
Installation
41+
------------
42+
* Require the bundle with composer:
43+
44+
``` bash
45+
composer require symfony-bundles/json-request-bundle
46+
```
47+
48+
[package-link]: https://packagist.org/packages/symfony-bundles/json-request-bundle
49+
[license-link]: https://github.com/symfony-bundles/json-request-bundle/blob/master/LICENSE
50+
[license-image]: https://poser.pugx.org/symfony-bundles/json-request-bundle/license
51+
[testing-link]: https://travis-ci.org/symfony-bundles/json-request-bundle
52+
[testing-image]: https://travis-ci.org/symfony-bundles/json-request-bundle.svg?branch=master
53+
[stable-image]: https://poser.pugx.org/symfony-bundles/json-request-bundle/v/stable
54+
[downloads-image]: https://poser.pugx.org/symfony-bundles/json-request-bundle/downloads
55+
[sensiolabs-insight-link]: https://insight.sensiolabs.com/projects/dea68633-2368-4e12-a516-89157d2c6b07
56+
[sensiolabs-insight-image]: https://insight.sensiolabs.com/projects/dea68633-2368-4e12-a516-89157d2c6b07/big.png
57+
[code-coverage-link]: https://scrutinizer-ci.com/g/symfony-bundles/json-request-bundle/?branch=master
58+
[code-coverage-image]: https://scrutinizer-ci.com/g/symfony-bundles/json-request-bundle/badges/coverage.png?b=master
59+
[scrutinizer-code-quality-link]: https://scrutinizer-ci.com/g/symfony-bundles/json-request-bundle/?branch=master
60+
[scrutinizer-code-quality-image]: https://scrutinizer-ci.com/g/symfony-bundles/json-request-bundle/badges/quality-score.png?b=master
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class SymfonyBundlesJsonRequestBundle extends Bundle
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function getContainerExtension()
13+
{
14+
return new DependencyInjection\JsonRequestExtension();
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle\Tests\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Processor;
6+
use SymfonyBundles\JsonRequestBundle\Tests\TestCase;
7+
use Symfony\Component\Config\Definition\ConfigurationInterface;
8+
use SymfonyBundles\JsonRequestBundle\DependencyInjection\Configuration;
9+
10+
class ConfigurationTest extends TestCase
11+
{
12+
public function testConfiguration()
13+
{
14+
$processor = new Processor();
15+
$configuration = new Configuration();
16+
17+
$this->assertInstanceOf(ConfigurationInterface::class, $configuration);
18+
19+
$configs = $processor->processConfiguration($configuration, []);
20+
21+
$this->assertArraySubset([], $configs);
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace SymfonyBundles\JsonRequestBundle\Tests\DependencyInjection;
4+
5+
use SymfonyBundles\JsonRequestBundle\Tests\TestCase;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use SymfonyBundles\JsonRequestBundle\DependencyInjection\JsonRequestExtension;
9+
10+
class JsonRequestExtensionTest extends TestCase
11+
{
12+
public function testHasListener()
13+
{
14+
$container = new ContainerBuilder();
15+
$extension = new JsonRequestExtension();
16+
$listenerService = 'sb_json_request.request_transformer';
17+
18+
$this->assertInstanceOf(Extension::class, $extension);
19+
20+
$extension->load([], $container);
21+
22+
$this->assertTrue($container->has($listenerService));
23+
}
24+
25+
public function testAlias()
26+
{
27+
$extension = new JsonRequestExtension();
28+
29+
$this->assertStringEndsWith('json_request', $extension->getAlias());
30+
}
31+
}

0 commit comments

Comments
 (0)