![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Ecombricks/Common/DataObject/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ declare(strict_types=1); namespace Ecombricks\Common\DataObject; /** * Object reflection */ class Reflection { /** * Class * * @var \ReflectionClass */ private $class; /** * Methods * * @var array */ private $methods = []; /** * Parent methods * * @var array */ private $parentMethods = []; /** * Properties * * @var array */ private $properties = []; /** * Constructor * * @param string $class * @return void */ public function __construct(string $class) { $this->class = new \ReflectionClass($class); } /** * Get parent method * * @param string $className * @param string $methodName * @return \ReflectionMethod */ public function getParentMethod(string $className, string $methodName): \ReflectionMethod { if (!empty($this->parentMethods[$className][$methodName])) { return $this->parentMethods[$className][$methodName]; } return $this->parentMethods[$className][$methodName] = $this->getClassMethod($this->getParentClass($className), $methodName); } /** * Get method * * @param string $methodName * @return \ReflectionMethod */ public function getMethod(string $methodName): \ReflectionMethod { if (!empty($this->methods[$methodName])) { return $this->methods[$methodName]; } return $this->methods[$methodName] = $this->getClassMethod($this->class, $methodName); } /** * Invoke method * * @param object $object * @param string $methodName * @param mixed ...$arguments * @return mixed */ public function invokeMethod($object, string $methodName, ...$arguments) { return $this->getMethod($methodName)->invoke($object, ...$arguments); } /** * Invoke parent method * * @param object $object * @param string $className * @param string $methodName * @param mixed ...$arguments * @return mixed */ public function invokeParentMethod($object, string $className, string $methodName, ...$arguments) { return $this->getParentMethod($className, $methodName)->invoke($object, ...$arguments); } /** * Get property * * @param string $propertyName * @return \ReflectionProperty */ public function getProperty(string $propertyName): \ReflectionProperty { if (array_key_exists($propertyName, $this->properties)) { return $this->properties[$propertyName]; } return $this->properties[$propertyName] = $this->getClassProperty($this->class, $propertyName); } /** * Get property value * * @param object $object * @param string $propertyName * @return mixed */ public function getPropertyValue($object, string $propertyName) { return $this->getProperty($propertyName)->getValue($object); } /** * Set property value * * @param object $object * @param string $propertyName * @param mixed $propertyValue * @return $this */ public function setPropertyValue($object, string $propertyName, $propertyValue) { $this->getProperty($propertyName)->setValue($object, $propertyValue); return $this; } /** * Get class method * * @param \ReflectionClass $class * @param string $methodName * @return \ReflectionMethod * @throws \Magento\Framework\Exception\LocalizedException */ private function getClassMethod(\ReflectionClass $class, string $methodName): \ReflectionMethod { $method = null; do { if ($class->hasMethod($methodName)) { $method = $class->getMethod($methodName); break; } } while ($class = $class->getParentClass()); if ($method === null) { throw new \Magento\Framework\Exception\LocalizedException(__('Method %1 is not found for %2 class.', $methodName, $class->getName())); } if (!$method->isPublic()) { $method->setAccessible(true); } return $method; } /** * Get parent class * * @param string $className * @return \ReflectionClass */ private function getParentClass(string $className): \ReflectionClass { $class = $this->class; $isClassFound = false; do { if ($class->getName() === $className) { $isClassFound = true; break; } } while ($class = $class->getParentClass()); if (!$isClassFound) { throw new \Magento\Framework\Exception\LocalizedException(__('Parent class %1 is not found.', $className)); } return $class; } /** * Get class property * * @param \ReflectionClass $class * @param string $propertyName * @return \ReflectionProperty * @throws \Magento\Framework\Exception\LocalizedException */ private function getClassProperty(\ReflectionClass $class, string $propertyName): \ReflectionProperty { $property = null; do { if ($class->hasProperty($propertyName)) { $property = $class->getProperty($propertyName); break; } } while ($class = $class->getParentClass()); if ($property === null) { throw new \Magento\Framework\Exception\LocalizedException(__('Property %1 is not found for %2 class.', $propertyName, $class->getName())); } if (!$property->isPublic()) { $property->setAccessible(true); } return $property; } }