Files
kiri-core/kiri-engine/Abstracts/Component.php
T

100 lines
1.8 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:28
*/
declare(strict_types=1);
namespace Kiri\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
2022-02-23 16:32:08 +08:00
use Kiri;
2022-02-23 18:02:29 +08:00
use Kiri\Error\StdoutLoggerInterface;
2022-01-09 03:50:38 +08:00
/**
* Class Component
2022-01-12 14:10:33 +08:00
* @package Kiri\Base
2022-01-09 03:50:38 +08:00
*/
class Component implements Configure
{
2022-02-23 18:02:29 +08:00
protected ?StdoutLoggerInterface $logger = null;
2022-02-23 16:32:08 +08:00
2022-01-09 03:50:38 +08:00
/**
* BaseAbstract constructor.
*
* @param array $config
* @throws Exception
*/
public function __construct(array $config = [])
{
2022-02-23 16:32:08 +08:00
if (is_null($this->logger)) {
2022-02-23 18:02:29 +08:00
$this->logger = Kiri::getDi()->get(StdoutLoggerInterface::class);
2022-02-23 16:32:08 +08:00
}
2022-01-09 03:50:38 +08:00
if (!empty($config) && is_array($config)) {
Kiri::configure($this, $config);
}
}
/**
* @throws Exception
*/
public function init()
{
}
/**
* @return string
*/
#[Pure] public static function className(): string
{
return static::class;
}
/**
2022-02-23 16:32:08 +08:00
* @param string $name
* @return mixed
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2022-02-23 16:32:08 +08:00
public function __get(string $name)
2022-01-09 03:50:38 +08:00
{
2022-02-23 16:32:08 +08:00
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
} else if (method_exists($this, $name)) {
return $this->{$name};
} else {
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
2022-01-09 03:50:38 +08:00
}
}
/**
2022-02-23 16:32:08 +08:00
* @param string $name
* @param $value
* @return void
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2022-02-23 16:32:08 +08:00
public function __set(string $name, $value): void
2022-01-09 03:50:38 +08:00
{
2022-02-23 16:32:08 +08:00
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} else if (method_exists($this, $name)) {
$this->{$name} = $value;
2022-01-09 03:50:38 +08:00
} else {
2022-02-23 16:32:08 +08:00
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
2022-01-09 03:50:38 +08:00
}
}
2022-02-23 16:32:08 +08:00
2022-01-09 03:50:38 +08:00
}