This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Introduce
ReflectionService
to improve performance (#18)
- Loading branch information
Showing
5 changed files
with
235 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/* | ||
* 2017 Romain CANON <romain.hydrocanon@gmail.com> | ||
* | ||
* This file is part of the TYPO3 Configuration Object project. | ||
* It is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License, either | ||
* version 3 of the License, or any later version. | ||
* | ||
* For the full copyright and license information, see: | ||
* http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace Romm\ConfigurationObject\Core\Service; | ||
|
||
use Romm\ConfigurationObject\Exceptions\PropertyNotAccessibleException; | ||
use TYPO3\CMS\Core\SingletonInterface; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Reflection\ClassReflection; | ||
use TYPO3\CMS\Extbase\Reflection\PropertyReflection; | ||
|
||
/** | ||
* An abstraction for class reflection, which is used a lot by this API, to | ||
* reduce performance impact. | ||
*/ | ||
class ReflectionService implements SingletonInterface | ||
{ | ||
/** | ||
* @var ReflectionService | ||
*/ | ||
private static $instance; | ||
|
||
/** | ||
* @var ClassReflection[] | ||
*/ | ||
protected $classReflection = []; | ||
|
||
/** | ||
* @var PropertyReflection[] | ||
*/ | ||
protected $classAccessibleProperties = []; | ||
|
||
/** | ||
* @return ReflectionService | ||
*/ | ||
public static function get() | ||
{ | ||
if (null === self::$instance) { | ||
self::$instance = GeneralUtility::makeInstance(self::class); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @return ClassReflection | ||
*/ | ||
public function getClassReflection($className) | ||
{ | ||
if (false === isset($this->classReflection[$className])) { | ||
$this->classReflection[$className] = GeneralUtility::makeInstance(ClassReflection::class, $className); | ||
} | ||
|
||
return $this->classReflection[$className]; | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @return PropertyReflection | ||
*/ | ||
public function getAccessibleProperties($className) | ||
{ | ||
if (false === isset($this->classAccessibleProperties[$className])) { | ||
$this->classAccessibleProperties[$className] = []; | ||
|
||
$classReflection = $this->getClassReflection($className); | ||
$properties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED); | ||
|
||
foreach ($properties as $property) { | ||
$this->classAccessibleProperties[$className][$property->getName()] = $property; | ||
} | ||
} | ||
|
||
return $this->classAccessibleProperties[$className]; | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @param string $propertyName | ||
* @return bool | ||
*/ | ||
public function isClassPropertyAccessible($className, $propertyName) | ||
{ | ||
$accessibleProperties = $this->getAccessibleProperties($className); | ||
|
||
return isset($accessibleProperties[$propertyName]); | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @param string $propertyName | ||
* @return PropertyReflection | ||
* @throws PropertyNotAccessibleException | ||
*/ | ||
public function getClassAccessibleProperty($className, $propertyName) | ||
{ | ||
if (false === $this->isClassPropertyAccessible($className, $propertyName)) { | ||
throw new PropertyNotAccessibleException( | ||
"Property $className::$propertyName is not accessible!", | ||
1489149795 | ||
); | ||
} | ||
|
||
$accessibleProperties = $this->getAccessibleProperties($className); | ||
|
||
return $accessibleProperties[$propertyName]; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
namespace Romm\ConfigurationObject\Tests\Fixture\Reflection; | ||
|
||
class ExampleReflection | ||
{ | ||
public $foo; | ||
|
||
protected $bar; | ||
|
||
private $baz; | ||
} |
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,96 @@ | ||
<?php | ||
namespace Romm\ConfigurationObject\Tests\Unit\Core\Service\Cache; | ||
|
||
use Romm\ConfigurationObject\Core\Service\ReflectionService; | ||
use Romm\ConfigurationObject\Exceptions\PropertyNotAccessibleException; | ||
use Romm\ConfigurationObject\Tests\Fixture\Reflection\ExampleReflection; | ||
use Romm\ConfigurationObject\Tests\Unit\AbstractUnitTest; | ||
use TYPO3\CMS\Extbase\Reflection\ClassReflection; | ||
|
||
class ReflectionServiceTest extends AbstractUnitTest | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function classReflectionIsInstantiatedOnlyOnce() | ||
{ | ||
$service = new ReflectionService; | ||
$classReflection = $service->getClassReflection(\stdClass::class); | ||
$classReflectionBis = $service->getClassReflection(\stdClass::class); | ||
$classReflectionTer = $service->getClassReflection(self::class); | ||
$this->assertSame($classReflection, $classReflectionBis); | ||
$this->assertNotSame($classReflection, $classReflectionTer); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function accessiblePropertiesAreAccessed() | ||
{ | ||
$service = new ReflectionService; | ||
$accessibleProperties = $service->getAccessibleProperties(ExampleReflection::class); | ||
$this->assertEquals( | ||
['foo', 'bar'], | ||
array_keys($accessibleProperties) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function accessiblePropertiesAreCalculatedOnce() | ||
{ | ||
/** @var ReflectionService|\PHPUnit_Framework_MockObject_MockObject $service */ | ||
$service = $this->getMockBuilder(ReflectionService::class) | ||
->setMethods(['getClassReflection']) | ||
->getMock(); | ||
|
||
$classReflection = new ClassReflection(ExampleReflection::class); | ||
|
||
$service->expects($this->once()) | ||
->method('getClassReflection') | ||
->willReturn($classReflection); | ||
|
||
$service->getAccessibleProperties(ExampleReflection::class); | ||
$service->getAccessibleProperties(ExampleReflection::class); | ||
$service->getAccessibleProperties(ExampleReflection::class); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function accessibleProperty() | ||
{ | ||
$service = new ReflectionService; | ||
|
||
$this->assertTrue($service->isClassPropertyAccessible(ExampleReflection::class, 'foo')); | ||
$this->assertTrue($service->isClassPropertyAccessible(ExampleReflection::class, 'bar')); | ||
$this->assertFalse($service->isClassPropertyAccessible(ExampleReflection::class, 'baz')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function singlePropertyReflectionAreReturned() | ||
{ | ||
$service = new ReflectionService; | ||
|
||
$fooReflection = $service->getClassAccessibleProperty(ExampleReflection::class, 'foo'); | ||
$this->assertEquals('foo', $fooReflection->getName()); | ||
|
||
$barReflection = $service->getClassAccessibleProperty(ExampleReflection::class, 'bar'); | ||
$this->assertEquals('bar', $barReflection->getName()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function notAccessiblePropertyThrowsException() | ||
{ | ||
$this->setExpectedException(PropertyNotAccessibleException::class); | ||
|
||
$service = new ReflectionService; | ||
|
||
$service->getClassAccessibleProperty(ExampleReflection::class, 'baz'); | ||
} | ||
} |