Skip to content

Commit

Permalink
Nette 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and Milan Felix Šulc committed Jun 28, 2019
1 parent e4b5a15 commit 1d5c773
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 111 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ Nette-based console commands for latte, DIC, security, utils and many others.

## Versions

| State | Version | Branch | PHP |
|-------------|----------|----------|----------|
| dev | `^0.5` | `master` | `>= 7.1` |
| stable | `^0.4` | `master` | `>= 7.1` |
| stable | `^0.1` | `master` | `>= 5.6` |
| State | Version | Branch | Nette | PHP |
|-------------|---------|----------|-------|---------|
| dev | `^0.6` | `master` | 3.0+ | `^7.2` |
| stable | `^0.5` | `master` | 3.0+ | `^7.2` |
| stable | `^0.4` | `master` | 2.4 | `>=7.1` |
| stable | `^0.1` | `master` | 2.4 | `>=5.6` |

## Maintainers

Expand Down
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
}
],
"require": {
"php": ">= 7.1",
"nette/di": "~2.4.11|~3.0",
"php": "^7.2",
"contributte/di": "^0.4.0",
"symfony/console": "^4.0.0"
},
"require-dev": {
"latte/latte": "~2.4.7|~2.6",
"nette/application": "~2.4.9|~3.0",
"nette/bootstrap": "~2.4.6|~3.0",
"nette/caching": "~3.0",
"nette/security": "~2.4.3|~3.0",
"nette/utils": "~2.5.1|~3.0",
"latte/latte": "~2.5.1",
"nette/application": "~3.0.0",
"nette/bootstrap": "~3.0.0",
"nette/caching": "~3.0.0",
"nette/security": "~3.0.0",
"ninjify/nunjuck": "^0.2.0",
"ninjify/qa": "^0.9.0",
"phpstan/extension-installer": "^1.0",
Expand Down Expand Up @@ -59,7 +58,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.5.x-dev"
"dev-master": "0.6.x-dev"
}
}
}
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ parameters:
- '#Parameter \$memcaches of method Contributte\\Console\\Extra\\Cache\\Cleaners\\MemcachedCleaner::__construct\(\) has invalid typehint type Memcached\.#'
- '#Call to method flush\(\) on an unknown class Memcache\.#'
- '#Call to method flush\(\) on an unknown class Memcached\.#'
- '~^Call to deprecated method validateConfig\(\) of class Nette\\DI\\CompilerExtension\.$~'
65 changes: 57 additions & 8 deletions src/DI/AdvancedCacheConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,78 @@

use Contributte\Console\Extra\Command\AdvancedCache\CacheCleanCommand;
use Contributte\Console\Extra\Command\AdvancedCache\CacheGenerateCommand;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;

/**
* @property-read stdClass $config
*/
class AdvancedCacheConsoleExtension extends CompilerExtension
{

/** @var mixed[] */
private $defaults = [
'cleaners' => [],
'generators' => [],
];
public static function createSchema(): Schema
{
return Expect::structure([
'cleaners' => Expect::arrayOf(
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
),
'generators' => Expect::arrayOf(
Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))
),
]);
}

public function getConfigSchema(): Schema
{
return self::createSchema();
}

public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);
$config = $this->config;
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);

// Register generators
$generatorDefinitions = [];

foreach ($config->generators as $generatorName => $generatorConfig) {
$generatorPrefix = $this->prefix('generator.' . $generatorName);
$generatorDefinition = $definitionsHelper->getDefinitionFromConfig($generatorConfig, $generatorPrefix);

if ($generatorDefinition instanceof Definition) {
$generatorDefinition->setAutowired(false);
}

$generatorDefinitions[] = $generatorDefinition;
}

$builder->addDefinition($this->prefix('generatorCommand'))
->setFactory(CacheGenerateCommand::class)
->setArguments([$config['generators']]);
->setArguments([$generatorDefinitions]);

// Register cleaners
$cleanerDefinitions = [];

foreach ($config->cleaners as $cleanerName => $cleanerConfig) {
$cleanerPrefix = $this->prefix('cleaner.' . $cleanerName);
$cleanerDefinition = $definitionsHelper->getDefinitionFromConfig($cleanerConfig, $cleanerPrefix);

if ($cleanerDefinition instanceof Definition) {
$cleanerDefinition->setAutowired(false);
}

$cleanerDefinitions[] = $cleanerDefinition;
}

$builder->addDefinition($this->prefix('cleanCommand'))
->setFactory(CacheCleanCommand::class)
->setArguments([$config['cleaners']]);
->setArguments([$cleanerDefinitions]);
}

}
35 changes: 22 additions & 13 deletions src/DI/CacheConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,40 @@
use Contributte\Console\Extra\Command\Cache\CachePurgeCommand;
use Nette\DI\CompilerExtension;
use Nette\DI\Helpers;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;

/**
* @property-read stdClass $config
*/
final class CacheConsoleExtension extends CompilerExtension
{

/** @var mixed[] */
private $defaults = [
'purge' => [
'%tempDir%/cache',
],
];
public static function createSchema(): Schema
{
return Expect::structure([
'purge' => Expect::listOf('string'),
]);
}

public function getConfigSchema(): Schema
{
return self::createSchema();
}

public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->config;

// Don't use predefined default values, if user provide it
if (isset($this->config['purge'])) {
$this->defaults['purge'] = [];
// Default values cannot be in schema, arrays are merged by keys
if ($config->purge === []) {
$config->purge = Helpers::expand(['%tempDir%/cache'], $builder->parameters);
}

$config = $this->validateConfig($this->defaults);
$config = Helpers::expand($config, $builder->parameters);

$builder->addDefinition($this->prefix('purge'))
->setFactory(CachePurgeCommand::class, [$config['purge']]);
->setFactory(CachePurgeCommand::class, [$config->purge]);
}

}
50 changes: 22 additions & 28 deletions src/DI/ConsoleBridgesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\ClassType;
use Nette\Utils\Validators;
use Nette\Schema\Expect;
use Nette\Schema\Schema;

/**
* @property-read mixed[] $config
*/
final class ConsoleBridgesExtension extends CompilerExtension
{

/** @var mixed[] */
private $defaults = [
'advancedCache' => [],
'cache' => [],
'caching' => [],
'di' => [],
'latte' => [],
'router' => [],
'security' => [],
'utils' => [],
];
public function getConfigSchema(): Schema
{
return Expect::structure([
'advancedCache' => Expect::anyOf(false, AdvancedCacheConsoleExtension::createSchema())->default([]),
'cache' => Expect::anyOf(false, CacheConsoleExtension::createSchema())->default([]),
'caching' => Expect::anyOf(false)->default([]),
'di' => Expect::anyOf(false, DIConsoleExtension::createSchema())->default([]),
'latte' => Expect::anyOf(false, LatteConsoleExtension::createSchema())->default([]),
'router' => Expect::anyOf(false)->default([]),
'security' => Expect::anyOf(false)->default([]),
'utils' => Expect::anyOf(false)->default([]),
])->castTo('array');
}

/** @var string[] */
private $map = [
Expand All @@ -36,43 +42,31 @@ final class ConsoleBridgesExtension extends CompilerExtension
/** @var CompilerExtension[] */
private $passes = [];

/**
* Register services
*/
public function loadConfiguration(): void
{
$config = $this->validateConfig($this->defaults, $this->config);
$config = $this->config;

foreach ($config as $bridge => $enabled) {
foreach ($config as $bridge => $bridgeConfig) {
// Don't register sub extension
if ($enabled === false) {
if ($bridgeConfig === false) {
continue;
}

// Security check
Validators::assertField($config, $bridge, 'array');

// Register sub extension a.k.a CompilerPass
$this->passes[$bridge] = new $this->map[$bridge]();
$this->passes[$bridge]->setCompiler($this->compiler, $this->prefix($bridge));
$this->passes[$bridge]->setConfig($this->config[$bridge] ?? []);
$this->passes[$bridge]->setConfig($bridgeConfig);
$this->passes[$bridge]->loadConfiguration();
}
}

/**
* Decorate services
*/
public function beforeCompile(): void
{
foreach ($this->passes as $pass) {
$pass->beforeCompile();
}
}

/**
* Decorate initialize method
*/
public function afterCompile(ClassType $class): void
{
foreach ($this->passes as $pass) {
Expand Down
35 changes: 22 additions & 13 deletions src/DI/DIConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,40 @@
use Contributte\Console\Extra\Command\DI\DIPurgeCommand;
use Nette\DI\CompilerExtension;
use Nette\DI\Helpers;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;

/**
* @property-read stdClass $config
*/
final class DIConsoleExtension extends CompilerExtension
{

/** @var mixed[] */
private $defaults = [
'purge' => [
'%tempDir%/cache/Nette.Configurator',
],
];
public static function createSchema(): Schema
{
return Expect::structure([
'purge' => Expect::listOf('string'),
]);
}

public function getConfigSchema(): Schema
{
return self::createSchema();
}

public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->config;

// Don't use predefined default values, if user provide it
if (isset($this->config['purge'])) {
$this->defaults['purge'] = [];
// Default values cannot be in schema, arrays are merged by keys
if ($config->purge === []) {
$config->purge = Helpers::expand(['%tempDir%/cache/nette.configurator'], $builder->parameters);
}

$config = $this->validateConfig($this->defaults);
$config = Helpers::expand($config, $builder->parameters);

$builder->addDefinition($this->prefix('purge'))
->setFactory(DIPurgeCommand::class, [$config['purge']]);
->setFactory(DIPurgeCommand::class, [$config->purge]);
}

}
Loading

0 comments on commit 1d5c773

Please sign in to comment.