-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #97 Autodiscover resource's model interfaces and deprecate ex…
…plicit configuration (pamil) This PR was merged into the 1.6-dev branch. Discussion ---------- Fixes #94, solution described in #94 (comment). Commits ------- d31f45a Autodiscover resource's model interfaces and deprecate explicit configuration
- Loading branch information
Showing
13 changed files
with
371 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Bundle/DependencyInjection/Compiler/Helper/TargetEntitiesResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\Helper; | ||
|
||
use Sylius\Component\Resource\Model\ResourceInterface; | ||
|
||
final class TargetEntitiesResolver implements TargetEntitiesResolverInterface | ||
{ | ||
public function resolve(array $resources): array | ||
{ | ||
$interfaces = []; | ||
|
||
foreach ($resources as $alias => $configuration) { | ||
$model = $this->getModel($alias, $configuration); | ||
|
||
foreach (class_implements($model) as $interface) { | ||
if ($interface === ResourceInterface::class) { | ||
continue; | ||
} | ||
|
||
$interfaces[$interface][] = $model; | ||
} | ||
} | ||
|
||
$interfaces = array_filter($interfaces, function (array $classes): bool { | ||
return count($classes) === 1; | ||
}); | ||
|
||
$interfaces = array_map(function (array $classes): string { | ||
return (string) current($classes); | ||
}, $interfaces); | ||
|
||
foreach ($resources as $alias => $configuration) { | ||
if (isset($configuration['classes']['interface'])) { | ||
$model = $this->getModel($alias, $configuration); | ||
$interface = $configuration['classes']['interface']; | ||
|
||
@trigger_error( | ||
sprintf( | ||
'Specifying interface for resources is deprecated since ResourceBundle v1.6 and will be removed in v2.0. ' . | ||
'Please rely on autodiscovering entity interfaces instead. ' . | ||
'Triggered by resource "%s" with model "%s" and interface "%s".', | ||
$alias, | ||
$model, | ||
$interface | ||
), | ||
\E_USER_DEPRECATED | ||
); | ||
|
||
$interfaces[$interface] = $model; | ||
} | ||
} | ||
|
||
return $interfaces; | ||
} | ||
|
||
private function getModel(string $alias, array $configuration): string | ||
{ | ||
if (!isset($configuration['classes']['model'])) { | ||
throw new \InvalidArgumentException(sprintf('Could not get model class from resource "%s".', $alias)); | ||
} | ||
|
||
return $configuration['classes']['model']; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Bundle/DependencyInjection/Compiler/Helper/TargetEntitiesResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\Helper; | ||
|
||
interface TargetEntitiesResolverInterface | ||
{ | ||
/** | ||
* @return array Interface to class map. | ||
*/ | ||
public function resolve(array $resourcesConfiguration): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\Tests\Fixtures; | ||
|
||
use Sylius\Component\Resource\Model\ResourceInterface; | ||
|
||
interface AnimalInterface extends ResourceInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\Tests\Fixtures; | ||
|
||
final class Bear implements BearInterface | ||
{ | ||
public function getId(): int | ||
{ | ||
return 42; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\Tests\Fixtures; | ||
|
||
interface BearInterface extends MammalInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\Tests\Fixtures; | ||
|
||
final class Fly implements FlyInterface | ||
{ | ||
public function getId(): int | ||
{ | ||
return 42; | ||
} | ||
} |
Oops, something went wrong.