Skip to content

Commit

Permalink
Nette 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jun 13, 2019
1 parent 55328f7 commit aaecf15
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -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

Expand Down
11 changes: 4 additions & 7 deletions composer.json
Expand Up @@ -17,11 +17,11 @@
}
],
"require": {
"php": ">= 7.1",
"apitte/core": "^0.6.0"
"php": "^7.2",
"apitte/core": "dev-next"
},
"require-dev": {
"nette/di": "~2.4.13",
"nette/di": "~3.0.0-stable",
"ninjify/nunjuck": "^0.2.0",
"ninjify/qa": "^0.8.0",
"phpstan/extension-installer": "^1.0",
Expand All @@ -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"
}
}
}
69 changes: 39 additions & 30 deletions src/DI/NegotiationPlugin.php
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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 aaecf15

Please sign in to comment.