Files
kiri-core/note/Inject.php
T

106 lines
2.1 KiB
PHP
Raw Normal View History

2021-02-22 17:44:24 +08:00
<?php
namespace Annotation;
2021-03-31 18:37:53 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Core\Str;
use Kiri\Kiri;
2021-08-13 16:57:12 +08:00
use ReflectionException;
use ReflectionProperty;
2021-02-22 17:44:24 +08:00
/**
* Class Inject
* @package Annotation
*/
2021-03-03 18:35:04 +08:00
#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject extends Attribute
2021-02-22 17:44:24 +08:00
{
2021-07-27 16:08:16 +08:00
/**
* Inject constructor.
* @param string $value
2021-08-18 11:20:42 +08:00
* @param array $construct
2021-07-27 16:08:16 +08:00
*/
2021-08-29 04:06:49 +08:00
public function __construct(string $value, array $construct = [])
2021-08-01 15:25:59 +08:00
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws ReflectionException
* @throws Exception
*/
2021-08-29 04:06:49 +08:00
public static function execute(mixed $params, mixed $class, mixed $method = null): bool
2021-08-01 15:25:59 +08:00
{
2021-08-29 04:06:49 +08:00
if (!($method = static::getProperty($class, $method))) {
2021-08-01 15:25:59 +08:00
return false;
}
/** @var ReflectionProperty $class */
2021-08-29 04:06:49 +08:00
$injectValue = static::parseInjectValue($params);
2021-08-01 15:25:59 +08:00
if ($method->isPrivate() || $method->isProtected()) {
2021-08-29 04:06:49 +08:00
static::setter($class, $method, $injectValue);
2021-08-01 15:25:59 +08:00
} else {
$class->{$method->getName()} = $injectValue;
}
return true;
}
/**
* @param $class
* @param $method
* @param $injectValue
*/
2021-08-29 04:06:49 +08:00
private static function setter($class, $method, $injectValue)
2021-08-01 15:25:59 +08:00
{
$method = 'set' . ucfirst(Str::convertUnderline($method->getName()));
if (!method_exists($class, $method)) {
return;
}
$class->$method($injectValue);
}
/**
* @param $class
* @param $method
* @return ReflectionProperty|bool
2021-08-13 15:19:07 +08:00
* @throws ReflectionException
2021-08-01 15:25:59 +08:00
*/
2021-08-29 04:06:49 +08:00
private static function getProperty($class, $method): ReflectionProperty|bool
2021-08-01 15:25:59 +08:00
{
if ($method instanceof ReflectionProperty && !$method->isStatic()) {
return $method;
}
if (is_object($class)) $class = $class::class;
2021-08-11 01:04:57 +08:00
$method = Kiri::getDi()->getClassReflectionProperty($class, $method);
2021-08-01 15:25:59 +08:00
if (!$method || $method->isStatic()) {
return false;
}
return $method;
}
/**
* @return mixed
* @throws Exception
*/
2021-08-29 04:06:49 +08:00
private static function parseInjectValue($params): mixed
2021-08-01 15:25:59 +08:00
{
2021-08-29 04:06:49 +08:00
if (!Kiri::app()->has($params->value)) {
if (!empty($params->construct)) {
return Kiri::getDi()->newObject($params->value, $params->construct);
2021-08-18 11:20:42 +08:00
}
2021-08-29 04:06:49 +08:00
return Kiri::getDi()->get($params->value);
2021-08-01 15:25:59 +08:00
} else {
2021-08-29 04:06:49 +08:00
return Kiri::app()->get($params->value);
2021-08-01 15:25:59 +08:00
}
}
2021-03-23 18:00:49 +08:00
2021-02-22 17:44:24 +08:00
}