Files
kiri-core/kiri-engine/Runtime.php
T

102 lines
1.8 KiB
PHP
Raw Normal View History

2021-04-07 23:50:11 +08:00
<?php
2021-08-11 01:04:57 +08:00
namespace Kiri;
2021-04-07 23:50:11 +08:00
2021-04-14 17:47:10 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Input;
2021-09-02 12:00:26 +08:00
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2021-04-07 23:50:11 +08:00
/**
2021-04-09 09:55:30 +08:00
* Class Runtime
2021-08-11 01:04:57 +08:00
* @package Kiri
2021-04-07 23:50:11 +08:00
*/
class Runtime extends Command
{
2021-05-18 10:28:04 +08:00
public string $command = 'runtime:builder';
2021-04-07 23:50:11 +08:00
2021-05-18 10:28:04 +08:00
public string $description = 'create app file cache';
2021-04-09 09:55:30 +08:00
2021-04-07 23:50:11 +08:00
2021-05-18 10:28:04 +08:00
const CACHE_NAME = '.runtime.cache';
const CONFIG_NAME = '.config.cache';
2021-05-04 03:43:22 +08:00
2021-09-02 12:00:26 +08:00
protected function configure()
{
$this->setName('runtime:builder');
}
2021-04-09 09:51:17 +08:00
/**
2021-09-02 12:00:26 +08:00
* @param InputInterface $input
* @param OutputInterface $output
2021-09-02 14:58:08 +08:00
* @return int
2021-04-14 17:47:10 +08:00
* @throws Exception
2021-04-09 09:51:17 +08:00
*/
2021-09-02 14:55:40 +08:00
public function execute(InputInterface $input, OutputInterface $output): int
2021-05-18 10:28:04 +08:00
{
// TODO: Implement onHandler() method.
2021-08-11 01:04:57 +08:00
$annotation = Kiri::app()->getAnnotation();
2021-05-18 10:28:04 +08:00
$runtime = storage(static::CACHE_NAME);
2021-05-18 14:10:39 +08:00
$config = storage(static::CONFIG_NAME);
2021-05-18 10:28:04 +08:00
2021-08-11 01:04:57 +08:00
Kiri::writeFile($config, $this->configEach());
Kiri::writeFile($runtime, serialize($annotation->getLoader()));
2021-05-18 14:15:50 +08:00
2021-09-02 14:55:40 +08:00
return 1;
2021-05-18 10:28:04 +08:00
}
/**
2021-05-18 14:10:39 +08:00
* @return string
2021-05-18 10:28:04 +08:00
* @throws Exception
*/
2021-05-18 14:10:39 +08:00
public function configEach(): string
2021-05-18 10:28:04 +08:00
{
$array = [];
2021-08-11 01:04:57 +08:00
$configs = Kiri::app()->getConfig();
2021-05-18 10:28:04 +08:00
foreach ($configs->getData() as $key => $datum) {
if ($datum instanceof \Closure) {
continue;
}
if (is_array($datum)) {
$array[$key] = $this->arrayEach($datum);
} else {
$array[$key] = $datum;
}
}
2021-05-18 14:10:39 +08:00
return serialize($array);
2021-05-18 10:28:04 +08:00
}
/**
* @param array $value
* @return array
*/
private function arrayEach(array $value): array
{
$array = [];
foreach ($value as $key => $item) {
if ($item instanceof \Closure) {
continue;
}
if (is_array($item)) {
$array[$key] = $this->arrayEach($item);
} else {
$array[$key] = $item;
}
}
return $array;
}
2021-04-07 23:50:11 +08:00
}