Skip to content

Commit

Permalink
Requires nette/di ^3.0 (BC BREAK)
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Jul 11, 2023
1 parent 6e4ee4c commit 000efc4
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
tests:
uses: janpecha/actions/.github/workflows/nette-tester-library.yml@master
with:
phpVersions: '["7.2", "7.3", "7.4", "8.0", "8.1"]'
phpVersions: '["7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]'
lowestDependencies: true

coding-style:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ src_dir = src/
tester_bin = vendor/bin/tester
tests_dir = tests/
coverage_name = $(tests_dir)coverage.html
php_bin = php8.1
php_bin = php
phpstan_bin = phpstan

.PHONY: test coverage clean phpstan
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"description": "LeanMapper extension for Nette DI",
"require": {
"php": ">=7.2.0",
"nette/di": "^2.4",
"nette/di": "^3.0",
"inlm/mappers": "^2.1 || ^3.0",
"tharos/leanmapper": "^4.0",
"czproject/assert": "^1.3"
},
"require-dev": {
"nette/tester": "^2.0",
"nette/bootstrap": "^2.4",
"tracy/tracy": "^2.4"
"nette/bootstrap": "^3.0",
"tracy/tracy": "^2.6"
},
"license": "BSD-3-Clause",
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ composer require janpecha/leanmapper-extension

Extension requires:
* PHP 7.2 or later
* Nette 2.4 or later
* Nette 3.0 or later
* LeanMapper 4.0 or later


Expand Down
174 changes: 113 additions & 61 deletions src/LeanMapperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use CzProject\Assert\Assert;
use Inlm\Mappers;
use Nette\DI\ServiceDefinition;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\ContainerBuilder;
use Nette\Schema\Expect;
use Nette\Utils\Strings;
use Nette;

Expand All @@ -18,31 +19,6 @@ class LeanMapperExtension extends \Nette\DI\CompilerExtension
const NAME_MAPPING_CAMELCASE = 'camelcase';
const NAME_MAPPING_UNDERSCORE = 'underscore';

/** @var array<string, mixed> */
public $defaults = [
// services
'mapper' => TRUE,
'entityFactory' => \LeanMapper\DefaultEntityFactory::class,
'connection' => \LeanMapper\Connection::class,

// mapper
'defaultEntityNamespace' => 'Model\\Entity',

// connection
'host' => 'localhost',
'driver' => 'mysqli',
'username' => NULL,
'password' => NULL,
'database' => NULL,
'lazy' => TRUE,
'charset' => 'utf8mb4',

// mapper
'nameMapping' => self::NAME_MAPPING_CAMELCASE,
'entityMapping' => NULL,
'prefix' => NULL,
];

/** @var array<string, class-string> */
private $nameMappers = [
self::NAME_MAPPING_DEFAULT => Mappers\DefaultMapper::class,
Expand All @@ -51,16 +27,105 @@ class LeanMapperExtension extends \Nette\DI\CompilerExtension
];


public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::structure([
// services
'mapper' => Expect::bool()
->default(TRUE),

'entityFactory' => Expect::anyOf(
Expect::string(),
FALSE
)
->default(\LeanMapper\DefaultEntityFactory::class),

'connection' => Expect::anyOf(
Expect::string(),
FALSE
)
->default(\LeanMapper\Connection::class),

// mapper
'defaultEntityNamespace' => Expect::string()
->default('Model\\Entity'),

// connection
'driver' => Expect::string()
->default('mysqli'),

'host' => Expect::string()
->nullable()
->default('localhost'),

'username' => Expect::string()
->nullable()
->default(NULL),

'password' => Expect::string()
->nullable()
->default(NULL),

'database' => Expect::string()
->nullable()
->default(NULL),

'lazy' => Expect::bool()
->default(TRUE),

'charset' => Expect::string()
->default('utf8mb4')
->nullable(),

// mapper
'nameMapping' => Expect::anyOf(
self::NAME_MAPPING_CAMELCASE,
self::NAME_MAPPING_DEFAULT,
self::NAME_MAPPING_UNDERSCORE
)
->default(self::NAME_MAPPING_CAMELCASE),

'entityMapping' => Expect::arrayOf(
Expect::anyOf(
Expect::string(),
Expect::structure([
'entity' => Expect::string()->required(),
'repository' => Expect::string()->nullable(),
'primaryKey' => Expect::string()->nullable(),
])->castTo('array')
),
Expect::string()
)
->default([]),

'prefix' => Expect::string()
->default(NULL)
->nullable(),

'profiler' => Expect::bool()
->default(NULL),
])
->before(function (array $config) {
// config alias for 'username'
if (!isset($config['username']) && isset($config['user'])) {
$config['username'] = $config['user'];
}

unset($config['user']);
return $config;
})
->castTo('array');
}


public function loadConfiguration()
{
$config = $this->getConfig($this->defaults);
$config = $this->getConfig();
$builder = $this->getContainerBuilder();

// config alias for 'username'
if (!isset($config['username']) && isset($config['user'])) {
$config['username'] = $config['user'];
if (!is_array($config)) {
throw new \RuntimeException("Config must be array, " . gettype($config) . ' given.');
}
unset($config['user']);

// use profiler?
$useProfiler = isset($config['profiler'])
Expand All @@ -87,9 +152,8 @@ public function loadConfiguration()
/**
* Adds connection service into container
* @param array<string, mixed> $config
* @return ServiceDefinition|NULL
*/
protected function configConnection(ContainerBuilder $builder, array $config)
protected function configConnection(ContainerBuilder $builder, array $config): ?ServiceDefinition
{
$connectionClass = $config['connection'];

Expand Down Expand Up @@ -119,9 +183,8 @@ protected function configConnection(ContainerBuilder $builder, array $config)
/**
* Adds connection service into container
* @param array<string, mixed> $config
* @return ServiceDefinition|NULL
*/
protected function configEntityFactory(ContainerBuilder $builder, array $config)
protected function configEntityFactory(ContainerBuilder $builder, array $config): ?ServiceDefinition
{
$entityFactoryClass = $config['entityFactory'];

Expand All @@ -141,9 +204,8 @@ protected function configEntityFactory(ContainerBuilder $builder, array $config)
/**
* Adds mapper service into container
* @param array<string, mixed> $config
* @return ServiceDefinition|NULL
*/
protected function configMapper(ContainerBuilder $builder, array $config)
protected function configMapper(ContainerBuilder $builder, array $config): ?ServiceDefinition
{
Assert::bool($config['mapper'], "Option 'mapper' must be bool");

Expand All @@ -161,7 +223,7 @@ protected function configMapper(ContainerBuilder $builder, array $config)
$mainMapper = $nameMapper;

$dynamicMapper = $builder->addDefinition($this->prefix('dynamicMapper'));
$usesDynamicMapper = $this->processEntityProviders($dynamicMapper, $config);
$usesDynamicMapper = $this->processEntityProviders($dynamicMapper);
$usesDynamicMapper = $this->processUserMapping($dynamicMapper, $config) || $usesDynamicMapper;

if ($usesDynamicMapper) {
Expand All @@ -182,8 +244,8 @@ protected function configMapper(ContainerBuilder $builder, array $config)
$mainMapper = $prefixMapper;
}

$mainMapper = $this->configureStiMapper($mainMapper, $config);
$mainMapper = $this->configureRowMapper($mainMapper, $config);
$mainMapper = $this->configureStiMapper($mainMapper);
$mainMapper = $this->configureRowMapper($mainMapper);

return $mainMapper;
}
Expand All @@ -192,11 +254,9 @@ protected function configMapper(ContainerBuilder $builder, array $config)
/**
* Processes user entities mapping + registers repositories in container
* @param array<string, mixed> $config
* @return bool
*/
protected function processUserMapping(ServiceDefinition $mapper, array $config)
protected function processUserMapping(ServiceDefinition $mapper, array $config): bool
{
$builder = $this->getContainerBuilder();
$usesMapping = FALSE;

if (isset($config['entityMapping'])) {
Expand Down Expand Up @@ -227,12 +287,9 @@ protected function processUserMapping(ServiceDefinition $mapper, array $config)
/**
* @see https://github.com/Kdyby/Doctrine/blob/6fc930a79ecadca326722f1c53cab72d56ee2a90/src/Kdyby/Doctrine/DI/OrmExtension.php#L255-L278
* @see http://forum.nette.org/en/18888-extending-extensions-solid-modular-concept
* @param array<string, mixed> $config
* @return bool
*/
protected function processEntityProviders(ServiceDefinition $mapper, array $config)
protected function processEntityProviders(ServiceDefinition $mapper): bool
{
$builder = $this->getContainerBuilder();
$usesMapping = FALSE;

foreach ($this->compiler->getExtensions() as $extension) {
Expand All @@ -258,11 +315,7 @@ protected function processEntityProviders(ServiceDefinition $mapper, array $conf
}


/**
* @param array<string, mixed> $config
* @return ServiceDefinition
*/
protected function configureStiMapper(ServiceDefinition $mainMapper, array $config)
protected function configureStiMapper(ServiceDefinition $mainMapper): ServiceDefinition
{
$builder = $this->getContainerBuilder();
$stiMapper = $builder->addDefinition($this->prefix('stiMapper'));
Expand Down Expand Up @@ -336,11 +389,7 @@ protected function configureStiMapper(ServiceDefinition $mainMapper, array $conf
}


/**
* @param array<string, mixed> $config
* @return ServiceDefinition
*/
protected function configureRowMapper(ServiceDefinition $mainMapper, array $config)
protected function configureRowMapper(ServiceDefinition $mainMapper): ServiceDefinition
{
$builder = $this->getContainerBuilder();
$rowMapper = $builder->addDefinition($this->prefix('rowMapper'));
Expand Down Expand Up @@ -449,9 +498,8 @@ protected function configureRowMapper(ServiceDefinition $mainMapper, array $conf
* Registers new entity in mapper
* @param array $mapping [table => '', primaryKey => '', entity => '', repository => '']
* @phpstan-param array<string, mixed> $mapping
* @return bool
*/
protected function registerInMapper(ServiceDefinition $mapper, array $mapping = NULL)
protected function registerInMapper(ServiceDefinition $mapper, array $mapping = NULL): bool
{
if ($mapping === NULL) {
return FALSE;
Expand Down Expand Up @@ -485,9 +533,13 @@ protected function registerInMapper(ServiceDefinition $mapper, array $mapping =
* @param string $name
* @return void
*/
public static function register(Nette\Configurator $configurator, $name = 'leanmapper')
public static function register(\Nette\Bootstrap\Configurator $configurator, $name = 'leanmapper')
{
$configurator->onCompile[] = function ($config, Nette\DI\Compiler $compiler) use ($name) {
if (!is_array($configurator->onCompile)) {
throw new \RuntimeException("Configurator::onCompile must be array, iterable " . gettype($configurator->onCompile) . ' given.');
}

$configurator->onCompile[] = function (\Nette\Bootstrap\Configurator $configurator, Nette\DI\Compiler $compiler) use ($name) {
$compiler->addExtension($name, new LeanMapperExtension());
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
leanmapper:
driver: fakeMysql
nameMapping: CaMelCaSe
nameMapping: camelcase
2 changes: 1 addition & 1 deletion tests/LeanMapperExtension/config/nameMapping.default.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
leanmapper:
driver: fakeMysql
nameMapping: DeFaUlT
nameMapping: default
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
leanmapper:
driver: fakeMysql
nameMapping: UnDerScOrE
nameMapping: underscore

0 comments on commit 000efc4

Please sign in to comment.