Skip to content
Permalink
Browse files

Nette 3

  • Loading branch information...
mabar authored and f3l1x committed Jun 13, 2019
1 parent 03338ac commit d74b21bd0acde24add2bb02d00a5e7cbee765023
Showing with 47 additions and 40 deletions.
  1. +6 −5 README.md
  2. +2 −5 composer.json
  3. +39 −30 src/DI/NegotiationPlugin.php
@@ -27,11 +27,12 @@ Transform response entity into response with unified format in dependence on `Ac

## Version

| State | Version | Branch | PHP | Composer |
|-------------|--------------|----------|----------|-------------------------------------------------|
| development | `^0.6.0` | `master` | `>= 7.1` | `minimum-stability: dev`, `prefer-stable: true` |
| stable | `^0.5.0` | `master` | `>= 7.1` | |
| stable | `^0.3.0` | `master` | `>= 5.6` | |
| State | Version | Branch | Nette | PHP |
|-------------|---------|----------|-------|---------|
| dev | `^0.7` | `master` | 3.0+ | `^7.2` |
| stable | `^0.6` | `master` | 3.0+ | `^7.2` |
| stable | `^0.5` | `master` | 2.4 | `>=7.1` |
| stable | `^0.3` | `master` | 2.4 | `>=5.6` |

## Maintainers

@@ -21,7 +21,7 @@
"apitte/core": "^0.6.0"
},
"require-dev": {
"nette/di": "~2.4.13",
"nette/di": "~3.0.0",
"ninjify/nunjuck": "^0.2.0",
"ninjify/qa": "^0.8.0",
"phpstan/extension-installer": "^1.0",
@@ -35,17 +35,14 @@
"Apitte\\Negotiation\\": "src"
}
},
"conflict": {
"nette/di": "<2.4.13"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "0.6.x-dev"
"dev-master": "0.7.x-dev"
}
}
}
@@ -4,9 +4,8 @@
use Apitte\Core\DI\ApiExtension;
use Apitte\Core\DI\Helpers;
use Apitte\Core\DI\Plugin\AbstractPlugin;
use Apitte\Core\DI\Plugin\CoreDecoratorPlugin;
use Apitte\Core\DI\Plugin\PluginCompiler;
use Apitte\Core\DI\Plugin\Plugin;
use Apitte\Core\Exception\Logical\InvalidStateException;
use Apitte\Negotiation\ContentNegotiation;
use Apitte\Negotiation\Decorator\ResponseEntityDecorator;
@@ -19,57 +18,63 @@
use Apitte\Negotiation\Transformer\JsonTransformer;
use Apitte\Negotiation\Transformer\JsonUnifyTransformer;
use Apitte\Negotiation\Transformer\RendererTransformer;
class NegotiationPlugin extends AbstractPlugin
use Nette\DI\Definitions\ServiceDefinition;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;
/**
* @property-read stdClass $config
*/
class NegotiationPlugin extends Plugin
{
public const PLUGIN_NAME = 'negotiation';
/** @var mixed[] */
protected $defaults = [
'unification' => false,
];
public static function getName(): string
{
return 'negotiation';
}
public function __construct(PluginCompiler $compiler)
protected function getConfigSchema(): Schema
{
parent::__construct($compiler);
$this->name = self::PLUGIN_NAME;
return Expect::structure([
'unification' => Expect::bool(false),
]);
}
/**
* Register services
*/
public function loadPluginConfiguration(): void
{
if ($this->compiler->getPlugin(CoreDecoratorPlugin::PLUGIN_NAME) === null) {
if ($this->compiler->getPlugin(CoreDecoratorPlugin::getName()) === null) {
throw new InvalidStateException(sprintf('Plugin "%s" must be enabled', CoreDecoratorPlugin::class));
}
$builder = $this->getContainerBuilder();
$config = $this->getConfig();
$config = $this->config;
$globalConfig = $this->compiler->getExtension()->getConfig();
$builder->addDefinition($this->prefix('transformer.json'))
->setFactory(JsonTransformer::class)
->addSetup('setDebugMode', [$globalConfig['debug']])
->addSetup('setDebugMode', [$globalConfig->debug])
->addTag(ApiExtension::NEGOTIATION_TRANSFORMER_TAG, ['suffix' => 'json'])
->setAutowired(false);
$builder->addDefinition($this->prefix('transformer.csv'))
->setFactory(CsvTransformer::class)
->addSetup('setDebugMode', [$globalConfig['debug']])
->addSetup('setDebugMode', [$globalConfig->debug])
->addTag(ApiExtension::NEGOTIATION_TRANSFORMER_TAG, ['suffix' => 'csv'])
->setAutowired(false);
$builder->addDefinition($this->prefix('transformer.fallback'))
->setFactory(JsonTransformer::class)
->addSetup('setDebugMode', [$globalConfig['debug']])
->addSetup('setDebugMode', [$globalConfig->debug])
->addTag(ApiExtension::NEGOTIATION_TRANSFORMER_TAG, ['suffix' => '*', 'fallback' => true])
->setAutowired(false);
$builder->addDefinition($this->prefix('transformer.renderer'))
->setFactory(RendererTransformer::class)
->addSetup('setDebugMode', [$globalConfig['debug']])
->addSetup('setDebugMode', [$globalConfig->debug])
->addTag(ApiExtension::NEGOTIATION_TRANSFORMER_TAG, ['suffix' => '#'])
->setAutowired(false);
@@ -92,18 +97,18 @@ public function loadPluginConfiguration(): void
->setFactory(ResponseEntityDecorator::class)
->addTag(ApiExtension::CORE_DECORATOR_TAG, ['priority' => 500]);
if ($config['unification'] === true) {
if ($config->unification) {
$builder->removeDefinition($this->prefix('transformer.fallback'));
$builder->removeDefinition($this->prefix('transformer.json'));
$builder->addDefinition($this->prefix('transformer.fallback'))
->setFactory(JsonUnifyTransformer::class)
->addSetup('setDebugMode', [$globalConfig['debug']])
->addSetup('setDebugMode', [$globalConfig->debug])
->addTag(ApiExtension::NEGOTIATION_TRANSFORMER_TAG, ['suffix' => '*', 'fallback' => true])
->setAutowired(false);
$builder->addDefinition($this->prefix('transformer.json'))
->setFactory(JsonUnifyTransformer::class)
->addSetup('setDebugMode', [$globalConfig['debug']])
->addSetup('setDebugMode', [$globalConfig->debug])
->addTag(ApiExtension::NEGOTIATION_TRANSFORMER_TAG, ['suffix' => 'json'])
->setAutowired(false);
}
@@ -129,8 +134,9 @@ protected function compileNegotiators(): void
$definitions = Helpers::sortByPriorityInTag(ApiExtension::NEGOTIATION_NEGOTIATOR_TAG, $definitions);
// Setup negotiators
$builder->getDefinition($this->prefix('negotiation'))
->setArguments([$definitions]);
$negotiationDefinition = $builder->getDefinition($this->prefix('negotiation'));
assert($negotiationDefinition instanceof ServiceDefinition);
$negotiationDefinition->setArguments([$definitions]);
}
protected function compileTransformers(): void
@@ -160,16 +166,19 @@ protected function compileTransformers(): void
}
// Obtain suffix negotiator
$suffixNegotiator = $builder->getDefinition($this->prefix('negotiator.suffix'));
$suffixNegotiator->setArguments([$transformers['suffix']]);
$suffixNegotiatorDefinition = $builder->getDefinition($this->prefix('negotiator.suffix'));
assert($suffixNegotiatorDefinition instanceof ServiceDefinition);
$suffixNegotiatorDefinition->setArguments([$transformers['suffix']]);
// Obtain default negotiator
$defaultNegotiator = $builder->getDefinition($this->prefix('negotiator.default'));
$defaultNegotiator->setArguments([$transformers['suffix']]);
$defaultNegotiatorDefinition = $builder->getDefinition($this->prefix('negotiator.default'));
assert($defaultNegotiatorDefinition instanceof ServiceDefinition);
$defaultNegotiatorDefinition->setArguments([$transformers['suffix']]);
// Obtain fallback negotiator
$fallbackNegotiator = $builder->getDefinition($this->prefix('negotiator.fallback'));
$fallbackNegotiator->setArguments([$transformers['fallback']]);
$fallbackNegotiatorDefinition = $builder->getDefinition($this->prefix('negotiator.fallback'));
assert($fallbackNegotiatorDefinition instanceof ServiceDefinition);
$fallbackNegotiatorDefinition->setArguments([$transformers['fallback']]);
}
}

0 comments on commit d74b21b

Please sign in to comment.
You can’t perform that action at this time.