-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[API] 1.8 api as opt #12819
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e4bb9b2
[API] add tests for disabling api
SirDomin 8c0dedb
[API] RouteResolver throws 404 when api is disabled
SirDomin e1f4b82
[Docs] add info how to enable api
SirDomin 5efe6b7
[API] kernel request event redirects to 404 for disabled api
SirDomin ed43e6b
[API] set api as disabled by default
SirDomin 30ac3aa
[API] add tests for disabling api
SirDomin 42176f1
[API] RouteResolver throws 404 when api is disabled
SirDomin 029ff34
[API] kernel request event redirects to 404 for disabled api
SirDomin 6cffd3e
[API] set api as disabled by default
SirDomin 2ba0478
[API] remove phpunit, set response on event
SirDomin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sylius_api: | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sylius_api: | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Sylius/Bundle/ApiBundle/EventSubscriber/KernelRequestEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Sylius/Bundle/ApiBundle/spec/EventSubscriber/KernelRequestEventSubscriberSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaultFalse()