Skip to content

Commit

Permalink
use mapping metadata resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Feb 3, 2018
1 parent cd6097c commit 89f5335
Show file tree
Hide file tree
Showing 35 changed files with 244 additions and 228 deletions.
24 changes: 12 additions & 12 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
* text=auto

.gitattributes export-ignore
.gitignore export-ignore
.editorconfig export-ignore
.php_cs export-ignore
.travis.yml export-ignore
.coveralls.yml export-ignore
.scrutinizer.yml export-ignore
.styleci.yml export-ignore
humbug.json.dist export-ignore
README.md export-ignore
CONTRIBUTING.md export-ignore
phpunit.xml.dist export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.editorconfig export-ignore
.php_cs export-ignore
.travis.yml export-ignore
.coveralls.yml export-ignore
.scrutinizer.yml export-ignore
.styleci.yml export-ignore
infection.json.dist export-ignore
README.md export-ignore
CONTRIBUTING.md export-ignore
phpunit.xml.dist export-ignore
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
!.gitignore
composer.lock
cghooks.lock
vendor/
*.cache
humbuglog.*
infection-log.*
build/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ script:
- if [[ -z $TEST_VERSION ]]; then composer phpunit ; fi

after_script:
- if [[ $TEST_VERSION ]]; then travis_retry php vendor/bin/coveralls --verbose ; fi
- if [[ $TEST_VERSION ]]; then travis_retry php vendor/bin/php-coveralls --verbose ; fi
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Thanks to this library, instead of configuring routes by hand one by one and inc
If you're familiar with how Doctrine defines entities mappings you'll feel at home with slim-routing because much as how Doctrine does route mappings are defined either

* On class annotations (in controller classes)
* In routing definition files, currently supported in PHP, Json, XML and YAML
* In routing definition files, currently supported in PHP, JSON, XML and YAML

> Routing gathering and compilation can be quite a heavy load process depending on how many classes/files and routes are defined. For this reason it's advised to always use [Slim's router cache](https://www.slimframework.com/docs/objects/application.html#slim-default-settings) on production applications and invalidate cache on deployment
> Routing gathering and compilation can be quite a heavy load process depending on how many classes/files and routes are defined, specially for annotations. For this reason it's advised to always use [Slim's router cache](https://www.slimframework.com/docs/objects/application.html#slim-default-settings) on production applications and invalidate cache on deployment
Route callbacks can now return `\Jgut\Slim\Routing\Response\ResponseTypeInterface` responses that will be later transformed into the mandatory `Psr\Message\ResponseInterface` in a way that lets you decouple view from controller

Expand Down Expand Up @@ -78,7 +78,7 @@ $container['router'] = function ($container) {
};

$app->get('/', function(ServerRequestInterface $request, ResponseInterface $response) {
return (new PayloadResponseType($response))->setPayload(['param' => 'value']);
return (new PayloadResponseType())->setResponse($response)->setPayload(['param' => 'value']);
});

$app->run();
Expand All @@ -87,7 +87,7 @@ $app->run();
### Configuration

* `sources` must be an array containing arrays of configurations to create MappingDriver objects:
* `type` one of \Jgut\Slim\Routing\Mapping\Driver\DriverInterface constants: `DRIVER_ANNOTATION`, `DRIVER_PHP`, `DRIVER_JSON`, `DRIVER_XML` or `DRIVER_YAML` **defaults to DRIVER_ANNOTATION if no driver**
* `type` one of \Jgut\Slim\Routing\Mapping\Driver\DriverFactory constants: `DRIVER_ANNOTATION`, `DRIVER_PHP`, `DRIVER_JSON`, `DRIVER_XML` or `DRIVER_YAML` **defaults to DRIVER_ANNOTATION if no driver**
* `path` a string path or array of paths to where mapping files are located (files or directories) **REQUIRED if no driver**
* `driver` an already created \Jgut\Slim\Routing\Mapping\Driver\DriverInterface object **REQUIRED if no type AND path**
* `placeholderAliases` array of additional placeholder aliases. There are some default aliases already available:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"require": {
"php": "^7.0",
"doctrine/annotations": "^1.4",
"juliangut/mapping": "~0.1",
"juliangut/mapping": "~0.2",
"slim/slim": "^3.8",
"symfony/polyfill-php71": "^1.0"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.1",
"friendsofphp/php-cs-fixer": "^2.0",
"humbug/humbug": "~1.0@dev",
"infection/infection": "^0.7.0",
"mikey179/vfsstream": "^1.6",
"phpmd/phpmd": "^2.0",
"phpmetrics/phpmetrics": "^2.0",
Expand Down Expand Up @@ -80,7 +80,7 @@
"phpunit": "phpunit",
"phpunit-coverage": "phpunit --coverage-html build/coverage",
"phpunit-clover": "phpunit --coverage-clover build/logs/clover.xml",
"humbug": "humbug",
"infection": "infection",
"qa": [
"@phplint",
"@phpcs",
Expand All @@ -99,7 +99,7 @@
"test": [
"@qa",
"@phpunit",
"@humbug"
"@infection"
]
},
"extra": {
Expand Down
3 changes: 1 addition & 2 deletions humbug.json.dist → infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
},
"timeout": 10,
"logs": {
"text": "humbuglog.txt",
"json": "humbuglog.json"
"text": "infection-log.txt"
}
}
118 changes: 95 additions & 23 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace Jgut\Slim\Routing;

use Jgut\Slim\Routing\Mapping\Driver\DriverInterface;
use Jgut\Mapping\Driver\DriverInterface;
use Jgut\Mapping\Metadata\MetadataResolver;
use Jgut\Slim\Routing\Mapping\Driver\DriverFactory;
use Jgut\Slim\Routing\Naming\NamingInterface;
use Jgut\Slim\Routing\Naming\SnakeCase;
use Jgut\Slim\Routing\Response\Handler\ResponseTypeHandlerInterface;
use Jgut\Slim\Routing\Route\Resolver;

/**
* Routing configuration.
Expand All @@ -42,6 +45,20 @@ class Configuration
'any' => '.+',
];

/**
* Metadata resolver.
*
* @var MetadataResolver
*/
protected $metadataResolver;

/**
* Route resolver.
*
* @var Resolver
*/
protected $routeResolver;

/**
* Naming strategy.
*
Expand Down Expand Up @@ -71,25 +88,24 @@ public function __construct($configurations = [])

$configs = array_keys(get_object_vars($this));

$unknownParameters = array_diff(array_keys($configurations), $configs);
if (count($unknownParameters) > 0) {
throw new \InvalidArgumentException(
sprintf(
'The following configuration parameters are not recognized: %s',
implode(', ', $unknownParameters)
)
);
}

foreach ($configs as $config) {
if (isset($configurations[$config])) {
switch ($config) {
case 'sources':
$this->setSources($configurations[$config]);
break;

case 'placeholderAliases':
$this->addPlaceholderAliases($configurations[$config]);
break;

case 'namingStrategy':
$this->setNamingStrategy($configurations[$config]);
break;

case 'responseHandlers':
$this->addResponseHandlers($configurations[$config]);
break;
}
$callback = [
$this,
$config === 'placeholderAliases' ? 'addPlaceholderAliases' : 'set' . ucfirst($config),
];

call_user_func($callback, $configurations[$config]);
}
}
}
Expand Down Expand Up @@ -197,6 +213,62 @@ public function addPlaceholderAlias(string $alias, string $pattern): self
return $this;
}

/**
* Get metadata resolver.
*
* @return MetadataResolver
*/
public function getMetadataResolver(): MetadataResolver
{
if ($this->metadataResolver === null) {
$this->metadataResolver = new MetadataResolver(new DriverFactory());
}

return $this->metadataResolver;
}

/**
* Set metadata resolver.
*
* @param MetadataResolver $metadataResolver
*
* @return static
*/
public function setMetadataResolver(MetadataResolver $metadataResolver): self
{
$this->metadataResolver = $metadataResolver;

return $this;
}

/**
* Get route resolver.
*
* @return Resolver
*/
public function getRouteResolver(): Resolver
{
if ($this->routeResolver === null) {
$this->routeResolver = new Resolver($this);
}

return $this->routeResolver;
}

/**
* Set route resolver.
*
* @param Resolver $routeResolver
*
* @return static
*/
public function setRouteResolver(Resolver $routeResolver): self
{
$this->routeResolver = $routeResolver;

return $this;
}

/**
* Get naming strategy.
*
Expand Down Expand Up @@ -236,16 +308,16 @@ public function getResponseHandlers(): array
}

/**
* Add response handlers.
*
* @param ResponseTypeHandlerInterface[]|string[] $handlers
* Set response handlers.
*
* @throws \InvalidArgumentException
* @param array $handlers
*
* @return static
*/
public function addResponseHandlers(array $handlers): self
public function setResponseHandlers(array $handlers): self
{
$this->sources = [];

foreach ($handlers as $responseType => $responseHandler) {
$this->addResponseHandler($responseType, $responseHandler);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Annotation driver.
*/
class AnnotationDriver extends AbstractAnnotationDriver implements DriverInterface
class AnnotationDriver extends AbstractAnnotationDriver
{
/**
* {@inheritdoc}
Expand Down
10 changes: 5 additions & 5 deletions src/Mapping/Driver/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ class DriverFactory extends AbstractDriverFactory
/**
* {@inheritdoc}
*/
protected static function getAnnotationDriver(array $paths): DriverInterface
protected function getAnnotationDriver(array $paths): DriverInterface
{
return new AnnotationDriver($paths, new AnnotationReader());
}

/**
* {@inheritdoc}
*/
protected static function getPhpDriver(array $paths): DriverInterface
protected function getPhpDriver(array $paths): DriverInterface
{
return new PhpDriver($paths);
}

/**
* {@inheritdoc}
*/
protected static function getXmlDriver(array $paths): DriverInterface
protected function getXmlDriver(array $paths): DriverInterface
{
return new XmlDriver($paths);
}

/**
* {@inheritdoc}
*/
protected static function getJsonDriver(array $paths): DriverInterface
protected function getJsonDriver(array $paths): DriverInterface
{
return new JsonDriver($paths);
}

/**
* {@inheritdoc}
*/
protected static function getYamlDriver(array $paths): DriverInterface
protected function getYamlDriver(array $paths): DriverInterface
{
return new YamlDriver($paths);
}
Expand Down
23 changes: 0 additions & 23 deletions src/Mapping/Driver/DriverInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Mapping/Driver/JsonDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* JSON mapping driver.
*/
class JsonDriver extends AbstractMappingDriver implements DriverInterface
class JsonDriver extends AbstractMappingDriver
{
use JsonMappingTrait;
use MappingTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/Driver/PhpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* PHP mapping driver.
*/
class PhpDriver extends AbstractMappingDriver implements DriverInterface
class PhpDriver extends AbstractMappingDriver
{
use PhpMappingTrait;
use MappingTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* XML mapping driver.
*/
class XmlDriver extends AbstractMappingDriver implements DriverInterface
class XmlDriver extends AbstractMappingDriver
{
use XmlMappingTrait;
use MappingTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* YAML mapping driver.
*/
class YamlDriver extends AbstractMappingDriver implements DriverInterface
class YamlDriver extends AbstractMappingDriver
{
use YamlMappingTrait;
use MappingTrait;
Expand Down
Loading

0 comments on commit 89f5335

Please sign in to comment.