This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better private properties handling
- Loading branch information
Showing
9 changed files
with
180 additions
and
8 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
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,81 @@ | ||
<?php | ||
|
||
namespace Jane\Bundle\AutoMapperBundle\DependencyInjection\Compiler; | ||
|
||
use Jane\Component\AutoMapper\Extractor\MapToContextPropertyInfoExtractorDecorator; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractor; | ||
|
||
class PropertyInfoPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->has('property_info')) { | ||
return; | ||
} | ||
|
||
$propertyInfoDefinition = $container->findDefinition('property_info'); | ||
|
||
$container->setDefinition( | ||
'automapper.property_info.reflection_extractor.inner', | ||
new Definition( | ||
ReflectionExtractor::class, | ||
[ | ||
'$accessFlags' => ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PROTECTED | ReflectionExtractor::ALLOW_PRIVATE, | ||
] | ||
) | ||
); | ||
|
||
$container->setDefinition( | ||
'automapper.property_info.reflection_extractor', | ||
new Definition( | ||
MapToContextPropertyInfoExtractorDecorator::class, | ||
[ | ||
new Reference('automapper.property_info.reflection_extractor.inner'), | ||
] | ||
) | ||
); | ||
|
||
$container->setDefinition( | ||
'automapper.property_info', | ||
new Definition( | ||
PropertyInfoExtractor::class, | ||
[ | ||
$this->replaceReflectionExtractor($propertyInfoDefinition->getArgument(0)), | ||
$this->replaceReflectionExtractor($propertyInfoDefinition->getArgument(1)), | ||
$this->replaceReflectionExtractor($propertyInfoDefinition->getArgument(2)), | ||
$this->replaceReflectionExtractor($propertyInfoDefinition->getArgument(3)), | ||
$this->replaceReflectionExtractor($propertyInfoDefinition->getArgument(4)), | ||
] | ||
) | ||
); | ||
|
||
$container->setDefinition( | ||
'automapper.property_info.cache', | ||
new Definition(PropertyInfoCacheExtractor::class, [ | ||
new Reference('.inner'), | ||
new Reference('cache.property_info'), | ||
]) | ||
)->setDecoratedService('automapper.property_info'); | ||
} | ||
|
||
private function replaceReflectionExtractor(IteratorArgument $extractors): IteratorArgument | ||
{ | ||
$newExtractors = []; | ||
|
||
/** @var Reference $extractor */ | ||
foreach ($extractors->getValues() as $extractor) { | ||
$newExtractors[] = (string) $extractor === 'property_info.reflection_extractor' | ||
? new Reference('automapper.property_info.reflection_extractor') | ||
: $extractor; | ||
} | ||
|
||
return new IteratorArgument($newExtractors); | ||
} | ||
} |
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
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,33 @@ | ||
<?php | ||
|
||
namespace AutoMapper\Bundle\tests\Fixtures; | ||
|
||
use Jane\Component\AutoMapper\Attribute\MapToContext; | ||
|
||
class ClassWithMapToContextAttribute | ||
{ | ||
public function __construct( | ||
private string $value, | ||
) { | ||
} | ||
|
||
public function getValue( | ||
#[MapToContext('prefix')] string $prefix, | ||
#[MapToContext('suffix')] string $suffix, | ||
): string { | ||
return "{$prefix}_{$this->value}_{$suffix}"; | ||
} | ||
|
||
public function getVirtualProperty( | ||
#[MapToContext('prefix')] string $prefix, | ||
#[MapToContext('suffix')] string $suffix, | ||
): string { | ||
return "{$prefix}_{$this->value}_{$suffix}"; | ||
} | ||
|
||
public function getPropertyWithDefaultValue( | ||
string $someVar = 'foo', | ||
): string { | ||
return $someVar; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace AutoMapper\Bundle\tests\Fixtures; | ||
|
||
class ClassWithPrivateProperty | ||
{ | ||
public function __construct(private string $foo) | ||
{ | ||
} | ||
|
||
private function getBar(): string | ||
{ | ||
return 'bar'; | ||
} | ||
} |
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