diff --git a/public/index.php b/public/index.php index fa4bae0..b99fc7b 100644 --- a/public/index.php +++ b/public/index.php @@ -1,116 +1,288 @@ pushHandler(new Handler\RotatingFileHandler('/logs/php.log')); -$handler = new Handler\NativeMailerHandler('jpvial@incoviba.cl', 'Incoviba Error', 'alert@incoviba.cl'); -$handler->setFormatter(new Formatter\HtmlFormatter()); -$logger->pushHandler(new Handler\FilterHandler($handler, Level::Error)); -$logger->pushProcessor(new Processor\PsrLogMessageProcessor()); -$logger->pushProcessor(new Processor\IntrospectionProcessor()); -$logger->pushProcessor(new Processor\WebProcessor()); -$logger->pushProcessor(new Processor\MemoryPeakUsageProcessor()); + protected float $time = 0; -Monolog\ErrorHandler::register($logger); + public function start() + { + if ($this->debug and $this->benchmark) { + $this->time = microtime(true); + } + } -if (config('app.debug') == true) { - if (config('app.benchmark') == true) { - $benchmark = (object) ['time' => microtime(true)]; + public function stop() + { + if ($this->debug and $this->benchmark) { + $this->time = microtime(true) - $this->time; + } + } + + public function show() + { + if ($this->debug and $this->benchmark) { + $this->logger->debug("Time {$this->time}"); + echo view('benchmark', ['benchmark' => $this->time]); + } } } -Carbon::setLocale(config('app.locale')); -setlocale(LC_TIME, 'es_ES'); +class Request +{ + public function __construct() + { + sanitize(); + } -sanitize(); + protected array $server; + protected array $headers; + protected object $uri; + protected array $query; + protected object $body; -try { - Auth::isIn(); -} catch (PDOException $e) { - $logger->error($e); - header('Location: install'); - die(); + public function getServerParams(): array + { + return $this->server; + } + public function getHeaders(): array + { + return $this->headers; + } + public function getUri(): object + { + return $this->uri; + } + public function getQueryParams(): array + { + return $this->query; + } + public function getBody(): object + { + return $this->body; + } + + public static function createFromGlobals(): Request + { + $request = new Request(); + $request->server = $_SERVER; + $request->headers = apache_request_headers(); + $request->uri = (object) parse_url($_SERVER['REQUEST_URI']); + $request->query = $_GET; + $request->body = new class() { + public function __construct() + { + $this->contents = fopen('php://stdin', 'r'); + } + + protected $contents; + + public function getContent(): string + { + return stream_get_contents($this->contents); + } + }; + return $request; + } } +class Container +{ + public function __construct(array $definitions = []) + { + foreach ($definitions as $name => $value) + { + $this->set($name, $value); + } + if (!isset($definitions[Container::class])) { + $this->set(Container::class, $this); + } + } -try { - if (Auth::isIn()) { - if ((get('p') !== false or get('page') !== false or get('m') !== false or get('module') !== false)) { - if (($route = route()) !== false) { - echo $route; + protected array $definitions; + + public function get(string $name) + { + if (!isset($this->definitions[$name]) and class_exists($name)) { + $this->set($name, $name); + } + return $this->eval($this->definitions[$name]); + } + + public function set(string $name, $value): Container + { + $this->definitions[$name] = $value; + return $this; + } + + protected function eval($value) + { + if (is_callable($value)) { + return $this->evalCallable($value); + } + if (is_string($value) and class_exists($value)) { + return $this->evalNewClass($value); + } + return $value; + } + protected function evalCallable(callable $value) + { + $args = $this->getMethodParameters(new ReflectionFunction($value)); + return call_user_func_array($value, $args); + } + protected function evalNewClass(string $class) + { + $ref = new reflectionClass($class); + $args = $this->getMethodParameters($ref->getConstructor()); + return $ref->newInstanceArgs($args); + } + protected function getMethodParameters(ReflectionFunctionAbstract $function) + { + return array_map(function(ReflectionParameter $parameter) { + return $this->get($parameter->getType()->getName()); + }, $function->getParameters()); + } +} + +class App +{ + public function __construct( + protected Auth $authWrapper, + protected Request $request, + protected Monolog\Logger $logger, + protected Benchmark $benchmark, + ) {} + + public function run() + { + header("Access-Control-Allow-Origin: *"); + + Monolog\ErrorHandler::register($this->logger); + + $this->benchmark->start(); + + Carbon::setLocale(config('app.locale')); + setlocale(LC_TIME, 'es_ES'); + + try { + $this->authWrapper->isIn(); + } catch (PDOException $e) { + $this->logger->error($e); + header('Location: install'); + die(); + } + + $get = $this->request->getQueryParams(); + try { + if ($this->authWrapper->isIn()) { + if ((($get['p'] ?? false) !== false or ($get['page'] ?? false) !== false or ($get['m'] ?? false) !== false or ($get['module'] ?? false) !== false)) { + if (($route = route()) !== false) { + echo $route; + } else { + echo view('construccion'); + } + } else { + $proyectos = model(Proyecto::class)->findMany(); + $dias = []; + $cierres = []; + $pendientes = 0; + $hoy = 0; + foreach ($proyectos as $proyecto) { + $pendientes += $proyecto->cuotasPendientes(); + $hoy += $proyecto->cuotasHoy(); + foreach ($proyecto->cuotasMes() as $cuota) { + $f = $cuota->pago()->fecha(); + if ($f->isoWeekday() == 6 or $f->isoWeekDay() == 7) { + $f = $f->copy()->addDays(2)->startOfWeek(); + } + $dia = $f->format('Y-m-d'); + if (!isset($dias[$dia])) { + $dias[$dia] = [$proyecto->descripcion => 0]; + } + if (!isset($dias[$dia][$proyecto->descripcion])) { + $dias[$dia][$proyecto->descripcion] = 0; + } + $dias[$dia][$proyecto->descripcion] ++; + } + if (count($proyecto->cierres()) > 0) { + $cierres[$proyecto->descripcion] = (object) ['total' => count($proyecto->cierres()),'vigentes' => $proyecto->cierres(3), 'rechazados' => $proyecto->cierres(-1), 'pendientes' => $proyecto->cierres(2)]; + } + } + uksort($dias, function($a, $b) { + return strcmp($a, $b); + }); + uksort($cierres, function($a, $b) { + return strcmp($a, $b); + }); + echo view('home', compact('pendientes', 'hoy', 'dias', 'cierres')); + } + } elseif (($get['p'] ?? false) === 'auth' or ($get['page'] ?? false) === 'auth') { + $route = route(); + if ($route !== false) { + echo $route; + } else { + echo view('guest'); + } } else { - echo view('construccion'); + echo view('guest'); } - } else { - $proyectos = model(Proyecto::class)->findMany(); - $dias = []; - $cierres = []; - $pendientes = 0; - $hoy = 0; - foreach ($proyectos as $proyecto) { - $pendientes += $proyecto->cuotasPendientes(); - $hoy += $proyecto->cuotasHoy(); - foreach ($proyecto->cuotasMes() as $cuota) { - $f = $cuota->pago()->fecha(); - if ($f->isoWeekday() == 6 or $f->isoWeekDay() == 7) { - $f = $f->copy()->addDays(2)->startOfWeek(); - } - $dia = $f->format('Y-m-d'); - if (!isset($dias[$dia])) { - $dias[$dia] = [$proyecto->descripcion => 0]; - } - if (!isset($dias[$dia][$proyecto->descripcion])) { - $dias[$dia][$proyecto->descripcion] = 0; - } - $dias[$dia][$proyecto->descripcion] ++; - } - if (count($proyecto->cierres()) > 0) { - $cierres[$proyecto->descripcion] = (object) ['total' => count($proyecto->cierres()),'vigentes' => $proyecto->cierres(3), 'rechazados' => $proyecto->cierres(-1), 'pendientes' => $proyecto->cierres(2)]; - } - } - uksort($dias, function($a, $b) { - return strcmp($a, $b); - }); - uksort($cierres, function($a, $b) { - return strcmp($a, $b); - }); - echo view('home', compact('pendientes', 'hoy', 'dias', 'cierres')); + } catch (Exception $e) { + $this->logger->warning($e); + die(); + } catch (Error $e) { + $this->logger->error($e); + die(); } - } elseif (get('p') == 'auth' or get('page') == 'auth') { - $route = route(); - if ($route !== false) { - echo $route; - } else { - echo view('guest'); + + $this->benchmark->stop(); + + if (($get['ajax'] ?? false) !== '1' and ($get['p'] ?? false) !== 'ajax' and ($get['p'] ?? false) !== 'informes') { + $this->benchmark->show(); } - } else { - echo view('guest'); } -} catch (Exception $e) { - $logger->warning($e); - echo ""; -} catch (Error $e) { - $logger->error($e); - echo ""; } -if (config('app.debug') == 'true') { - if (config('app.benchmark') == 'true') { - $benchmark->time = microtime(true) - $benchmark->time; - $logger->debug("Time {$benchmark->time}"); - if (get('ajax') != '1' and get('p') != 'ajax' and get('p') != 'informes') { - echo view('benchmark', compact('benchmark')); - } - } -} -?> +$container = new Container([ + Monolog\Logger::class => function() { + return (new Monolog\Logger('global')) + ->pushHandler((new Handler\FilterHandler( + (new Handler\RotatingFileHandler('/logs/php.log')) + ->setFormatter(new Formatter\LineFormatter(null, null, true)), + Level::Debug, + Level::Notice + ))) + ->pushHandler((new Handler\FilterHandler( + (new Handler\NativeMailerHandler('jpvial@incoviba.cl', 'Error - Incoviba', 'alert@incoviba.cl')) + ->setFormatter(new Formatter\HtmlFormatter()), + Level::Warning, + Level::Emergency + ))) + ->pushProcessor(new Processor\PsrLogMessageProcessor()) + ->pushProcessor(new Processor\IntrospectionProcessor()) + ->pushProcessor(new Processor\HostnameProcessor()) + ->pushProcessor(new Processor\WebProcessor()) + ->pushProcessor(new Processor\MemoryPeakUsageProcessor()); + }, + Request::class => function() { + return Request::createFromGlobals(); + }, + Auth::class => function() { + return new Auth(); + }, + Benchmark::class => function(Container $container) { + return new Benchmark($container->get(Monolog\Logger::class), config('app.debug'), config('app.benchmark')); + } +]); + +$app = $container->get(App::class); +$app->run();