Skip to content

Commit

Permalink
Merge pull request #14 from 99designs/symfony5
Browse files Browse the repository at this point in the history
Add Symfony 5 support & drop Symfony 3 support
  • Loading branch information
sinamt authored Aug 27, 2023
2 parents 993f4fe + 5c73a48 commit 332dce4
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 30 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ jobs:
- name: Generate protobuf code
run: ./.github/workflows/protoc.sh

- name: Run test suite (Symfony 4)
run: SYMFONY_DEPRECATIONS_HELPER=weak ./vendor/bin/phpunit
- name: Run test suite (Symfony 5)
run: ./vendor/bin/phpunit

- name: Install Symfony 3
run: composer require --update-with-dependencies symfony/symfony ~3.4
- name: Install Symfony 4
run: composer require --update-with-dependencies symfony/symfony ~4.4

- name: Run test suite (Symfony 3)
- name: Run test suite (Symfony 4)
run: ./vendor/bin/phpunit

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
composer.lock
vendor
example/Haberdasher/var

.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AppKernel extends Kernel

```yaml
twirp_api:
resource: 'twirp.service_registry:loadRoutes'
resource: 'twirp.service_registry::loadRoutes'
type: service
prefix: /twirp
```
Expand Down
26 changes: 16 additions & 10 deletions TwirfonyBundle/Controller/TwirpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@
namespace Twirfony\TwirfonyBundle\Controller;

use Throwable;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Google\Protobuf\Internal\Message;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Twirfony\TwirfonyBundle\Event\ErrorEvent;
use Twirfony\TwirfonyBundle\Event\RequestEvent;
use Twirfony\TwirfonyBundle\Event\ResultEvent;
use Twirfony\TwirpError;

class TwirpController extends Controller
class TwirpController extends AbstractController implements LoggerAwareInterface
{
private $logger;

public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

public function rpcAction(Request $request)
{
$inputType = $request->attributes->get('inputType');
$serviceId = $request->attributes->get('service');
$service = $this->get($serviceId);
$service = $this->container->get($serviceId);
$method = $request->attributes->get('method');

$input = null;
Expand All @@ -34,9 +42,9 @@ public function rpcAction(Request $request)
$input->mergeFromString($request->getContent());
}

$this->get('event_dispatcher')->dispatch(RequestEvent::NAME, new RequestEvent($request, $serviceId, $method, $input));
$this->container->get('event_dispatcher')->dispatch(new RequestEvent($request, $serviceId, $method, $input), RequestEvent::NAME);
$output = $service->$method($input);
$this->get('event_dispatcher')->dispatch(ResultEvent::NAME, new ResultEvent($request, $serviceId, $method, $input, $output));
$this->container->get('event_dispatcher')->dispatch(new ResultEvent($request, $serviceId, $method, $input, $output), ResultEvent::NAME);

$response = new Response();

Expand All @@ -48,16 +56,14 @@ public function rpcAction(Request $request)
$response->headers->set('Content-Type', 'application/protobuf');
}
return $response;

} catch (TwirpError $e) {
$this->get('event_dispatcher')->dispatch(ErrorEvent::NAME, new ErrorEvent($request, $serviceId, $method, $input, $e));
$this->container->get('event_dispatcher')->dispatch(new ErrorEvent($request, $serviceId, $method, $input, $e), ErrorEvent::NAME);
return $this->errorResponse($e);

} catch (Throwable $e) {
$this->get('logger')->err($e->getMessage(), [
$this->logger->error($e->getMessage(), [
'exception' => $e
]);
$this->get('event_dispatcher')->dispatch(ErrorEvent::NAME, new ErrorEvent($request, $serviceId, $method, $input, $e));
$this->container->get('event_dispatcher')->dispatch(new ErrorEvent($request, $serviceId, $method, $input, $e), ErrorEvent::NAME);
return $this->errorResponse(TwirpError::internalErrorWith($e));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function loadRoutes()
'inputType' => $method->getInputType(),
'service' => $definition->getServiceId(),
'method' => $method->getPhpMethod(),
'_controller' => 'TwirfonyBundle:Twirp:rpc'
'_controller' => 'Twirfony\TwirfonyBundle\Controller\TwirpController::rpcAction'
]);
$routes->add('twirp_' . $method->getTwirpPath(), $route);
}
Expand Down
4 changes: 2 additions & 2 deletions TwirfonyBundle/Event/ErrorEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Twirfony\TwirfonyBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Google\Protobuf\Internal\Message;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\Event;

class ErrorEvent extends Event
{
Expand Down
4 changes: 2 additions & 2 deletions TwirfonyBundle/Event/RequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Twirfony\TwirfonyBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Google\Protobuf\Internal\Message;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\Event;

class RequestEvent extends Event
{
Expand Down
4 changes: 2 additions & 2 deletions TwirfonyBundle/Event/ResultEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Twirfony\TwirfonyBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Google\Protobuf\Internal\Message;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\Event;

class ResultEvent extends Event
{
Expand Down
8 changes: 7 additions & 1 deletion TwirfonyBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
services:

twirp.service_registry:
class: Twirfony\TwirfonyBundle\DependencyInjection\Twirp\ServiceRegistry
public: true
tags: [routing.route_loader]

Twirfony\TwirfonyBundle\Controller\TwirpController:
autoconfigure: true
autowire: true
calls:
- ["setContainer", ["@service_container"]]
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
]
},
"require": {
"php": "^7",
"php": ">=7",
"ext-json": "*",
"symfony/symfony": "~3|~4",
"symfony/symfony": "~4|~5",
"google/protobuf": "^3",
"psr/http-message": "^1",
"guzzlehttp/guzzle": "^6"
},
"require-dev": {
"phpunit/phpunit": "^7",
"phpunit/phpunit": "^9.6",
"symfony/phpunit-bridge": "^6.1"
}
}
3 changes: 2 additions & 1 deletion example/Haberdasher/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ parameters:
framework:
secret: '%secret%'
router:
resource: 'config/routing.yml'
resource: "%kernel.project_dir%/example/Haberdasher/app/config/routing.yml"
strict_requirements: ~
utf8: true
form: ~
default_locale: '%locale%'
trusted_hosts: ~
Expand Down
2 changes: 1 addition & 1 deletion example/Haberdasher/app/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
twirp_api:
resource: 'twirp.service_registry:loadRoutes'
resource: 'twirp.service_registry::loadRoutes'
type: service
prefix: /twirp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
use AppBundle\Twirp\HaberdasherInterface;
use AppBundle\Twirp\Hat;
use AppBundle\Twirp\Size;
use Twirfony\TwirpError;
use Twirfony\TwirpService;

class HaberdasherService implements TwirpService, HaberdasherInterface
{
public function makeHat(Size $size): Hat
{
if ($size->getInches() < 0) {
throw new \Exception();
}
if ($size->getInches() > 100) {
throw new TwirpError(TwirpError::INTERNAL, 'size too large');
}

return (new Hat)
->setInches($size->getInches())
->setColor("blue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace AppBundle\Service;

use AppBundle\Twirp\EmptyRequest;
use AppBundle\Twirp\HaberdasherClient;
use AppBundle\Twirp\HaberdasherException;
use AppBundle\Twirp\Size;
use Twirfony\GuzzleTestCase;

Expand All @@ -19,4 +21,26 @@ public function testIndex()
$this->assertEquals('Fedora', $hat->getName());
$this->assertEquals(2, $hat->getInches());
}

public function testException()
{
$this->expectException(HaberdasherException::class);
$this->expectExceptionMessage('');

$client = static::getClient('http://localhost/twirp/');
$client = new HaberdasherClient($client);

$client->makeHat((new Size)->setInches(-1));
}

public function testTwirpError()
{
$this->expectException(HaberdasherException::class);
$this->expectExceptionMessage('size too large');

$client = static::getClient('http://localhost/twirp/');
$client = new HaberdasherClient($client);

$client->makeHat((new Size)->setInches(101));
}
}

0 comments on commit 332dce4

Please sign in to comment.