Skip to content
Permalink
Browse files

Nette 3

  • Loading branch information...
mabar authored and f3l1x committed Jun 10, 2019
1 parent 102f2d9 commit b74ec0676058ebebc50e36ace2aa7f5474c8f7d7
@@ -16,14 +16,13 @@
}
],
"require": {
"php": ">= 7.1",
"contributte/psr7-http-message": "~0.5"
"php": "^7.2",
"contributte/di": "^0.4.0",
"contributte/psr7-http-message": "^0.6.0"
},
"require-dev": {
"nette/application": "~2.4.12",
"nette/di": "~2.4.12",
"nette/http": "~2.4.9",
"nette/utils": "~2.5.2",
"nette/application": "~3.0.0",
"nette/http": "~3.0.1",
"ninjify/nunjuck": "~0.2",
"ninjify/qa": "~0.8.0",
"phpstan/extension-installer": "^1.0",
@@ -34,16 +33,8 @@
"psr/log": "^1.0",
"tracy/tracy": "~2.6.1"
},
"conflict": {
"nette/application": "<2.4.12",
"nette/di": "<2.4.12",
"nette/http": "<2.4.9",
"nette/utils": "<2.5.2",
"tracy/tracy": "<2.5.4"
},
"suggest": {
"tracy/tracy": "to use TracyMiddleware",
"nette/di": "to use CompilerExtension(NetteMiddlewareExtension | StandaloneMiddlewareExtension)",
"nette/http": "to use NetteMiddlewareExtension & NetteMiddlewareApplication",
"nette/application": "to use PresenterMiddleware"
},
@@ -64,7 +55,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.8.x-dev"
"dev-master": "0.9.x-dev"
}
}
}
@@ -3,3 +3,7 @@ parameters:
- '#Only booleans are allowed in an if condition, mixed given.#'
- '#Only booleans are allowed in a negated boolean, mixed given.#'
- '#^Parameter \#2 \$parameters of function call_user_func_array expects array\<int, mixed\>, array\<int\|string, mixed\> given\.$#'
- '#^Parameter \#1 \$name of method Nette\\DI\\ContainerBuilder\:\:getDefinition\(\) expects string, int\|string given\.$#'
- '#^Only booleans are allowed in a negated boolean, int given\.$#'
# Cannot happen
- '#^Parameter \#1 \$request of method Contributte\\Middlewares\\PresenterMiddleware\:\:processRequest\(\) expects Nette\\Application\\Request, Nette\\Application\\Request\|null given\.$#'
@@ -2,42 +2,47 @@
namespace Contributte\Middlewares\DI;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Contributte\Middlewares\Exception\InvalidStateException;
use Contributte\Middlewares\Tracy\DebugChainBuilder;
use Contributte\Middlewares\Tracy\MiddlewaresPanel;
use Contributte\Middlewares\Utils\ChainBuilder;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Statement;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Utils\Validators;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;
/**
* @property-read stdClass $config
*/
abstract class AbstractMiddlewaresExtension extends CompilerExtension
{
public const MIDDLEWARE_TAG = 'middleware';
/** @var mixed[] */
protected $defaults = [
'middlewares' => [],
'debug' => false,
];
public function getConfigSchema(): Schema
{
return Expect::structure([
'middlewares' => Expect::arrayOf(
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
),
'debug' => Expect::bool(false),
]);
}
/**
* Register services
*/
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);
Validators::assertField($config, 'middlewares', 'array');
$config = $this->config;
// Register middleware chain builder
$chain = $builder->addDefinition($this->prefix('chain'))
->setAutowired(false);
if ($config['debug'] !== true) {
if (!$config->debug) {
$chain->setFactory(ChainBuilder::class);
} else {
$chain->setFactory(DebugChainBuilder::class);
@@ -47,16 +52,13 @@ public function loadConfiguration(): void
}
}
/**
* Decorate services
*/
public function beforeCompile(): void
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig();
$config = $this->config;
// Compile defined middlewares
if ($config['middlewares'] !== []) {
if ($config->middlewares !== []) {
$this->compileDefinedMiddlewares();
return;
@@ -75,26 +77,18 @@ public function beforeCompile(): void
private function compileDefinedMiddlewares(): void
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig();
$config = $this->config;
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);
// Obtain middleware chain builder
$chain = $builder->getDefinition($this->prefix('chain'));
assert($chain instanceof ServiceDefinition);
// Add middleware services to chain
$counter = 0;
foreach ($config['middlewares'] as $service) {
foreach ($config->middlewares as $service) {
// Create middleware as service
if (
is_array($service)
|| $service instanceof Statement
|| (is_string($service) && strncmp($service, '@', 1) !== 0)
) {
$def = $builder->addDefinition($this->prefix('middleware' . ($counter++)));
Compiler::loadDefinition($def, $service);
} else {
$def = $builder->getDefinition(ltrim($service, '@'));
}
$def = $definitionsHelper->getDefinitionFromConfig($service, $this->prefix('middleware' . ($counter++)));
// Append to chain of middlewares
$chain->addSetup('add', [$def]);
@@ -114,7 +108,7 @@ private function compileTaggedMiddlewares(): void
}
// Sort by priority
uasort($definitions, function ($a, $b) {
uasort($definitions, function (array $a, array $b) {
$p1 = $a['priority'] ?? 10;
$p2 = $b['priority'] ?? 10;
@@ -127,6 +121,7 @@ private function compileTaggedMiddlewares(): void
// Obtain middleware chain builder
$chain = $builder->getDefinition($this->prefix('chain'));
assert($chain instanceof ServiceDefinition);
// Add middleware services to chain
foreach ($definitions as $name => $tag) {
@@ -137,9 +132,9 @@ private function compileTaggedMiddlewares(): void
public function afterCompile(ClassType $class): void
{
$config = $this->validateConfig($this->defaults);
$config = $this->config;
if ($config['debug'] === true) {
if ($config->debug) {
$initialize = $class->getMethod('initialize');
$initialize->addBody(
'$this->getService(?)->addPanel($this->getService(?));',
@@ -3,7 +3,7 @@
namespace Contributte\Middlewares\DI;
use Contributte\Middlewares\Application\MiddlewareApplication;
use Nette\DI\Statement;
use Nette\DI\Definitions\Statement;
class MiddlewaresExtension extends AbstractMiddlewaresExtension
{
@@ -3,8 +3,8 @@
namespace Contributte\Middlewares\DI;
use Contributte\Middlewares\Application\NetteMiddlewareApplication;
use Nette\DI\Definitions\Statement;
use Nette\DI\ServiceCreationException;
use Nette\DI\Statement;
use Nette\Http\Request;
use Nette\Http\Response;

0 comments on commit b74ec06

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