Skip to content

Commit

Permalink
update files
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 authored Mar 13, 2024
1 parent 712111b commit 13aaa69
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ final class App extends Container
*/
public function __construct()
{
$this->setApp();
$this->loadEnv();
$this->configureEnvironment();
$this->registerProviders();
$this->setApp();
$this->registerComponents();
$this->bootServices();
}

Expand All @@ -32,7 +32,7 @@ public function __construct()
*/
private function setApp()
{
$this->singleton('app', fn () => $this);
$this->singleton('app', $this);
}

/**
Expand Down Expand Up @@ -72,11 +72,11 @@ private function configureEnvironment()
* This method reads the service provider configurations from the `providers.php` file
* and registers them with the application container.
*/
public function registerProviders()
public function registerComponents()
{
$pathConfig = config('paths.providersPath') . DIRECTORY_SEPARATOR;
$providers = include $pathConfig . 'providers.php';
$this->services($providers);
$this->components($providers);
}

/**
Expand Down
36 changes: 16 additions & 20 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ class Config
*/
protected static $config = [];

/**
* default config path
*/
private const DEFAULT_DIR = APP_PATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;

/**
* Get config
*
* @param string $var fichero.sección.variable
* @throws Exception
* @return mixed
*/
public static function get($var)
public static function get(string $var)
{
$sections = explode('.', $var);
self::$config[$sections[0]] ??= self::load($sections[0]);
Expand All @@ -25,13 +29,12 @@ public static function get($var)
3 => self::$config[$sections[0]][$sections[1]][$sections[2]] ?? null,
2 => self::$config[$sections[0]][$sections[1]] ?? null,
1 => self::$config[$sections[0]] ?? null,
default => throw new \Exception('Máximo 3 niveles en Config::get(fichero.sección.variable), pedido: ' . $var)
default => throw new \Exception('Maximum 3 levels in Config::get(file.section.variable), order: ' . $var)
};
}

/**
* Get all configs
*
* @return array<array-key,mixed>
*/
public static function getAll()
Expand All @@ -42,51 +45,44 @@ public static function getAll()
/**
* Set variable in config
*
* @param string $var variable de configuración
* @param mixed $value valor para atributo
* @throws Exception
* @return void
*/
public static function set($var, $value)
public static function set(string $var, $value)
{
$sections = explode('.', $var);
match (count($sections)) {
3 => self::$config[$sections[0]][$sections[1]][$sections[2]] = $value,
2 => self::$config[$sections[0]][$sections[1]] = $value,
1 => self::$config[$sections[0]] = $value,
default => throw new \Exception('Máximo 3 niveles en Config::set(fichero.sección.variable), pedido: ' . $var)
default => throw new \Exception('Maximum 3 levels in Config::get(file.section.variable), order: ' . $var)
};
}

/**
* Read config file
*
* @param string $file archivo .php o .ini
* @param bool $force forzar lectura de .php o .ini
* @return array<array-key,mixed>
*/
public static function read($file, $force = false)
public function read(string $file, string $path = null, bool $force = false)
{
if ($force) {
return self::$config[$file] = self::load($file);
return self::$config[$file] = self::load($file, $path);
}

return self::$config[$file] ??= self::load($file);
return self::$config[$file] ??= self::load($file, $path);
}

/**
* Load config file
*
* @param string $file archivo
* @return array<array-key,mixed>
*/
private static function load(string $file): array
public static function load(string $name, string $path = null): array
{
if (is_file($fileConfig = APP_PATH . DIRECTORY_SEPARATOR
. 'config' . DIRECTORY_SEPARATOR . $file . '.php')) {
$path = $path ?? self::DEFAULT_DIR;
if (is_file($fileConfig = $path . $name . '.php')) {
return require $fileConfig;
}

throw new \Exception(sprintf('Error when opening the configuration file %s ', $fileConfig));
throw new \Exception(sprintf('Error when opening the configuration file [ %s ] ', $fileConfig));
}
}
26 changes: 11 additions & 15 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
class Container
{
private $storage = [];
private static $instance;

private $instances = [];

public static function getInstance()
{
if (!isset(static::$instance)) {
static::$instance = new self();
if (!isset(static::$instances)) {
static::$instances = new self();
}

return static::$instance;
return static::$instances;
}

public function bind(string $key, $value)
Expand All @@ -21,16 +20,13 @@ public function bind(string $key, $value)
return $value;
}

public function singleton(string $key, callable $value)
public function singleton(string $key, object $value)
{
$this->bind($key, function () use ($key, $value) {
static $instance;
$instance[$key] ??= $value($this);

return $instance[$key];
});
if (!$this->has($key)) {
return $this->bind($key, $value);
}

return $this;
return $this->storage[$key];
}

public function key(string $key)
Expand Down Expand Up @@ -59,7 +55,7 @@ public function isSingleton(string $key): bool
return $this->has($key);
}

public function get(string $key): object
public function get(string $key): object|false
{
if ($this->has($key)) {
$class = $this->storage[$key] ?? null;
Expand All @@ -79,7 +75,7 @@ public function set(string $key, $value): void
$this->storage[$this->key($key)] = $value;
}

public function services(array $values): void
public function components(array $values): void
{
$this->storage += $values;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class Controller
public function __construct()
{
$app = app();
$this->request = app('request', new Request());
$this->response = app('response', new Response());
$this->view = app('view', new View());
$this->request = $app->request;
$this->response = $app->response;
$this->view = $app->view;

$this->registerDefaultMiddleware();
}
Expand Down
9 changes: 5 additions & 4 deletions src/HandlerErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

function handlerErrors($errno, $errstr, $errfile, $errline)
{
if (error_reporting() === 0)
if (error_reporting() === 0)
return false;

if (php_sapi_name() === 'cli') {
Expand All @@ -28,8 +28,8 @@ function handlerErrors($errno, $errstr, $errfile, $errline)

function handlerException(Throwable $e)
{
if (error_reporting() === 0)
return false;
if (error_reporting() === 0)
return false;

$log = Config::get('app.initLogReportings');

Expand Down Expand Up @@ -80,7 +80,7 @@ function handleCliError($errno, $errstr, $errfile, $errline)
{
$e = new ErrorException($errstr, $errno, 1, $errfile, $errline);
Console\CLIException::handleCLIException($e);

$log = Config::get('app.initLogReportings');

if ($log === true) {
Expand Down Expand Up @@ -110,6 +110,7 @@ function handleWebException(Throwable $e)
$whoops = new Run();
$handler = new PrettyPageHandler();
$handler->setPageTitle("¡Oops! Ha ocurrido un error");
$handler->setEditor(env('DEBUGBAR_EDITOR'));
$whoops->pushHandler($handler);
$whoops->handleException($e);
}
11 changes: 10 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@
* @param mixed $default
* @return mixed
*/
function config(string $key)
function config(string $key = null, string $value = null)
{
if (is_null($key)) {
return new Config;
}

if (!is_null($key) && !is_null($value)) {
Config::set($key, $value);
return;
}

$config = Config::get($key);
return $config;
}
Expand Down

0 comments on commit 13aaa69

Please sign in to comment.