Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] 1.8 api as opt #12819

Merged
merged 10 commits into from
Jul 22, 2021
2 changes: 2 additions & 0 deletions UPGRADE-1.8.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# UPGRADE FROM `v1.8.4` TO `v1.8.6`

1. Api is disabled by default, to enable it you need to set flag ``sylius_api.enabled`` to ``true`` in ``app/config/packages/_sylius.yaml``.

1. Change configuration of new ApiBundle in your `config/packages/security.yaml` file:

```diff
Expand Down
3 changes: 3 additions & 0 deletions config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ imports:
parameters:
sylius_core.public_dir: '%kernel.project_dir%/public'

sylius_api:
enabled: false

sylius_shop:
product_grid:
include_all_descendants: true
2 changes: 2 additions & 0 deletions config/packages/test/_sylius.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sylius_api:
enabled: true
2 changes: 2 additions & 0 deletions config/packages/test_cached/_sylius.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sylius_api:
enabled: true
1 change: 1 addition & 0 deletions docs/api/unified_api/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Introduction

The new, unified Sylius API is still under development, that's why the whole ``ApiBundle`` is tagged with ``@experimental``.
This means that all code from ``ApiBundle`` is excluded from :doc:`Backward Compatibility Promise </book/organization/backward-compatibility-promise>`.
You can enable entire API by changing the flag ``sylius_api.enabled`` to ``true`` in ``app/config/packages/_sylius.yaml``.

We have decided that we should rebuild our API and use API Platform to build a truly mature, multi-purpose API
which can define a new standard for headless e-commerce backends.
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<referencedMethod name="Symfony\Component\Intl\Intl::getLocaleBundle" />
<referencedMethod name="Symfony\Component\Intl\Intl::getRegionBundle" />
<referencedMethod name="Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent::getException" />
<referencedMethod name="Faker\Generator::__get"/>
</errorLevel>
</DeprecatedMethod>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function getConfigTreeBuilder(): TreeBuilder
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
->booleanNode('enabled')
->defaultTrue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultFalse()

->end()
->end()
;

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function load(array $configs, ContainerBuilder $container): void
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$container->setParameter('sylius_api.enabled', $config['enabled']);

$loader->load('services.xml');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\EventSubscriber;

use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/** @experimental */
final class KernelRequestEventSubscriber implements EventSubscriberInterface
{
/** @var bool */
private $apiEnabled;

/** @var string */
private $apiRoute;

public function __construct(bool $apiEnabled, string $apiRoute)
{
$this->apiEnabled = $apiEnabled;
$this->apiRoute = $apiRoute;
}

public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['validateApi', EventPriorities::PRE_VALIDATE],
];
}

public function validateApi(RequestEvent $event): void
{
$pathInfo = $event->getRequest()->getPathInfo();

if ($this->apiEnabled === false && strpos($pathInfo, $this->apiRoute) !== false) {
$event->setResponse(new Response('Route not found', 404));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="sylius.api.kerner_request_event_subscriber" class="Sylius\Bundle\ApiBundle\EventSubscriber\KernelRequestEventSubscriber">
<argument>%sylius_api.enabled%</argument>
<argument>%sylius.security.new_api_route%</argument>
<tag name="kernel.event_subscriber" />
</service>
<service id="sylius.api.product_slug_event_subscriber" class="Sylius\Bundle\ApiBundle\EventSubscriber\ProductSlugEventSubscriber">
<argument type="service" id="sylius.generator.slug" />
<tag name="kernel.event_subscriber" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\EventSubscriber;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

final class KernelRequestEventSubscriberSpec extends ObjectBehavior
{
function let(): void
{
$this->beConstructedWith(true, '/new-api');
}

function it_does_nothing_if_api_is_enabled(
RequestEvent $event,
Request $request,
HttpKernelInterface $kernel
): void {
$event->getRequest()->willReturn($request);

$request->getPathInfo()->willReturn('/new-api/any-endpoint');

$event->setResponse(new Response('Route not found', 404))->shouldNotBeCalled();

$this->validateApi($event);
}

function it_returns_404_if_api_is_disabled(
RequestEvent $event,
Request $request,
HttpKernelInterface $kernel
): void {
$this->beConstructedWith(false, '/new-api');

$event->getRequest()->willReturn($request);

$request->getPathInfo()->willReturn('/new-api/any-endpoint');

$event->setResponse(new Response('Route not found', 404))->shouldBeCalled();

$this->validateApi($event);
}

function it_does_nothing_for_non_api_endpoints_when_api_is_disabled(
RequestEvent $event,
Request $request,
HttpKernelInterface $kernel
): void {
$this->beConstructedWith(false, '/new-api');

$event->getRequest()->willReturn($request);

$request->getPathInfo()->willReturn('/');

$event->setResponse(new Response('Route not found', 404))->shouldNotBeCalled();

$this->validateApi($event);
}
}