File manager - Edit - /home/opticamezl/www/newok/application.tar
Back
src/Application.php 0000644 00000007540 15174212603 0010316 0 ustar 00 <?php namespace YOOtheme; use Psr\Http\Message\ResponseInterface; use YOOtheme\Configuration\Configuration; use YOOtheme\Http\Request; use YOOtheme\Http\Response; /** * @property Response $response * @property Request $request */ class Application extends Container { /** * @var Config */ protected $config; /** * @var array */ protected $loaders = []; /** * @var static|null */ protected static $instance; /** * Constructor. * * @param string $cache */ public function __construct($cache = null) { $this->config = new Configuration($cache); $this->set(static::class, $this); $this->setAlias(static::class, 'app'); $this->set(Config::class, $this->config); $this->setAlias(Config::class, 'config'); } /** * Gets global application. * * @param null|mixed $cache * * @return static */ public static function getInstance($cache = null) { return static::$instance ??= new static($cache); } /** * Run application. * * @param bool $send * * @return ResponseInterface */ public function run($send = true) { try { $response = Event::emit('app.request|middleware', [$this, 'handle'], $this->request); } catch (\Exception $exception) { $response = Event::emit('app.error|filter', $this->response, $exception); } return $send ? $response->send() : $response; } /** * Handles a request. * * @param Request $request * * @throws \Exception * * @return Response */ public function handle(Request $request) { $this->set(Request::class, $request); $route = $request->getAttribute('route'); $result = $this->call($route->getCallable()); if ($result instanceof Response) { return $result; } if (is_string($result) || (is_object($result) && method_exists($result, '__toString'))) { return $this->response->write((string) $result); } return $this->response; } /** * Loads a bootstrap file. * * @param string $files * * @return $this */ public function load($files) { $configs = []; foreach (File::glob($files, GLOB_NOSORT) as $file) { $configs = static::loadFile($file, $configs, ['app' => $this]); } if (isset($configs['loaders'])) { $this->loaders = array_merge($this->loaders, ...$configs['loaders']); } foreach (array_intersect_key($this->loaders, $configs) as $name => $loader) { if (is_string($loader) && class_exists($loader)) { $loader = $this->loaders[$name] = $this->resolveLoader($loader); } $loader($this, $configs[$name]); } return $this; } /** * Resolves a service instance. * * @param string $id * * @throws \Exception * @throws \ReflectionException * * @return mixed */ protected function resolveService($id) { return Hook::call([$id, 'app.resolve'], fn($id) => parent::resolveService($id), $id, $this); } /** * Resolves a loader instance. */ protected function resolveLoader(string $loader): callable { return new $loader($this); } /** * Loads a bootstrap config. */ protected static function loadFile(string $file, array $configs, array $parameters = []): array { extract($parameters, EXTR_SKIP); if (!is_array($config = require $file)) { throw new \RuntimeException("Unable to load file '{$file}'"); } foreach ($config as $key => $value) { $configs[$key][] = $value; } return $configs; } } src/Application/RouteLoader.php 0000644 00000000743 15174212603 0012541 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Container; class RouteLoader { /** * Load routes. * * @param Container $container * @param array $configs */ public function __invoke(Container $container, array $configs) { $container->extend('routes', static function ($routes) use ($configs) { foreach (array_merge(...$configs) as $route) { $routes->map(...$route); } }); } } src/Application/EventLoader.php 0000644 00000002725 15174212603 0012526 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Container; use YOOtheme\Event; use YOOtheme\EventDispatcher; class EventLoader { /** * @var EventDispatcher */ protected $dispatcher; /** * Constructor. */ public function __construct() { $this->dispatcher = Event::getDispatcher(); } /** * Load event listeners. */ public function __invoke(Container $container, array $configs) { foreach ($configs as $events) { foreach ($events as $event => $listeners) { foreach ($listeners as $class => $parameters) { $parameters = (array) $parameters; if (is_string($parameters[0])) { $parameters = [$parameters]; } foreach ($parameters as $params) { $this->addListener($container, $event, $class, ...$params); } } } } } /** * Adds a listener. */ public function addListener( Container $container, string $event, string $class, string $method, ...$params ): void { $this->dispatcher->addListener( $event, fn(...$arguments) => $container->call( $method[0] === '@' ? $class . $method : [$class, $method], $arguments, ), ...$params, ); } } src/Application/AliasLoader.php 0000644 00000001002 15174212603 0012461 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Container; class AliasLoader { /** * Load service aliases. * * @param Container $container * @param array $configs */ public function __invoke(Container $container, array $configs) { $config = array_merge_recursive(...$configs); foreach ($config as $id => $aliases) { foreach ((array) $aliases as $alias) { $container->setAlias($id, $alias); } } } } src/Application/ConfigLoader.php 0000644 00000004215 15174212603 0012646 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Config; use YOOtheme\ConfigObject; use YOOtheme\Container; use YOOtheme\Event; use YOOtheme\Hook; class ConfigLoader { /** * @var Config */ protected $config; /** * @var array */ protected $services = []; /** * Constructor. */ public function __construct(Container $container) { $this->config = $container->get(Config::class); Hook::after('app.resolve', [$this, 'loadConfig']); } /** * Load configuration. */ public function __invoke(Container $container, array $configs) { foreach ($configs as $config) { if ($config instanceof \Closure) { $config = $config($this->config, $container); } elseif (is_array($config)) { $config = $this->loadArray($config); } $this->config->add('', (array) $config); } } /** * After resolve service. * * @param mixed $service */ public function loadConfig($service, string $id) { if (!$service instanceof ConfigObject) { return; } foreach ($this->services[$id] ?? [] as $value) { $service->merge(is_string($value) ? static::loadFile($value) : $value); } Event::emit($id, $service); } protected function loadArray(array $config) { foreach ($config as $key => $value) { if (str_contains($key, '\\') && str_ends_with($key, 'Config')) { $this->services[$key][] = $value; unset($config[$key]); } } return $config; } protected static function loadFile(string $file) { $type = pathinfo($file, PATHINFO_EXTENSION); if ($type === 'php') { return require $file; } if ($type === 'ini') { return parse_ini_file($file, true, INI_SCANNER_TYPED); } if ($type === 'json') { return json_decode(file_get_contents($file), true); } throw new \RuntimeException("Unable to load config file '{$file}'"); } } src/Application/ExtendLoader.php 0000644 00000000711 15174212603 0012665 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Container; class ExtendLoader { /** * Load service extenders. * * @param Container $container * @param array $configs */ public function __invoke(Container $container, array $configs) { foreach ($configs as $config) { foreach ($config as $id => $callback) { $container->extend($id, $callback); } } } } src/Application/ServiceLoader.php 0000644 00000002123 15174212603 0013035 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Container; class ServiceLoader { /** * Load services configuration. * * @param Container $container * @param array $configs */ public function __invoke(Container $container, array $configs) { $config = array_merge(...$configs); foreach ($config as $id => $service) { if (is_array($service)) { $definition = $container->add($id); if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['factory'])) { $definition->setFactory($service['factory']); } if (isset($service['arguments'])) { $definition->setArguments($service['arguments']); } if (isset($service['shared'])) { $definition->setShared($service['shared']); } } else { $container->add($id, $service); } } } } src/Storage.php 0000644 00000004017 15174212603 0007453 0 ustar 00 <?php namespace YOOtheme; abstract class Storage implements \JsonSerializable { /** * @var array */ protected $values = []; /** * @var bool */ protected $modified = false; /** * Gets a value (shortcut). * * @param string $key * @param mixed $default * * @return mixed */ public function __invoke($key, $default = null) { return Arr::get($this->values, $key, $default); } /** * Checks if a key exists. * * @param string $key * * @return bool */ public function has($key) { return Arr::has($this->values, $key); } /** * Gets a value. * * @param string $key * @param mixed $default * * @return mixed */ public function get($key, $default = null) { return Arr::get($this->values, $key, $default); } /** * Sets a value. * * @param string $key * @param mixed $value * * @return $this */ public function set($key, $value) { Arr::set($this->values, $key, $value); $this->modified = true; return $this; } /** * Deletes a value. * * @param string $key * * @return $this */ public function del($key) { Arr::del($this->values, $key); $this->modified = true; return $this; } /** * Checks if values are modified. * * @return bool */ public function isModified() { return $this->modified; } /** * Gets values which should be serialized to JSON. * * @return array */ #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->values; } /** * Adds values from JSON. * * @param string $json * * @return $this */ protected function addJson($json) { $this->values = Arr::merge($this->values, json_decode($json, true) ?: []); return $this; } } bootstrap.php 0000644 00000001553 15174212603 0007277 0 ustar 00 <?php namespace YOOtheme\Application; use YOOtheme\Config; use YOOtheme\Path; use YOOtheme\Url; use YOOtheme\View; return [ 'extend' => [ Config::class => function (Config $config) { $config->addFilter( 'url', fn($value, $file) => Url::to(Path::resolve(dirname($file), $value)), ); }, View::class => function (View $view, $app) { $view->addGlobal('app', $app); $view->addGlobal('config', $app(Config::class)); }, ], 'aliases' => [ View::class => 'view', ], 'loaders' => [ 'services' => ServiceLoader::class, 'aliases' => AliasLoader::class, 'extend' => ExtendLoader::class, 'events' => EventLoader::class, 'routes' => RouteLoader::class, 'config' => ConfigLoader::class, ], ]; functions.php 0000644 00000000655 15174212603 0007274 0 ustar 00 <?php namespace YOOtheme; /** * Increase xdebug max nesting level. */ if ($level = ini_get('xdebug.max_nesting_level')) { ini_set('xdebug.max_nesting_level', max((int) $level, 256)); } /** * Gets a service from application. * * @param string $id * @param string ...$ids * * @return mixed */ function app($id = null, ...$ids) { $app = Application::getInstance(); return $id ? $app($id, ...$ids) : $app; }
| ver. 1.4 |
Github
|
.
| PHP 8.3.23 | Generation time: 0 |
proxy
|
phpinfo
|
Settings