Skip to content

Commit

Permalink
Stuff from the past...
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Nox committed May 17, 2022
1 parent 083585d commit c9fcad3
Show file tree
Hide file tree
Showing 32 changed files with 285 additions and 321 deletions.
11 changes: 5 additions & 6 deletions backend/class/Config/JsonConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use noxkiwi\core\Path;
use function array_replace_recursive;
use function compact;
use function is_array;
use const E_ERROR;

/**
Expand Down Expand Up @@ -44,11 +43,11 @@ public function __construct(string $file, bool $inherit = false)
if (empty($file)) {
throw new InvalidArgumentException('FILE_IS_EMPTY', E_ERROR);
}
$config = [];
$config = [];
if (! $inherit) {
$this->file = (string)$this->getFullPath($file);
$config = $this->decodeFile($this->file);
if (! is_array($config)) {
if (empty($config)) {
throw new ConfigurationException('CONTENT_IS_NOT_AN_ARRAY', E_ERROR, compact('file', 'config'));
}
$this->put($config);
Expand Down Expand Up @@ -88,12 +87,12 @@ private function getFullPath(string $file): ?string
*
* @param string $fullPath
*
* @return array|null
* @return array
*/
protected function decodeFile(string $fullPath): ?array
protected function decodeFile(string $fullPath): array
{
try {
return JsonHelper::decodeFileToArray($fullPath);
return (array)JsonHelper::decodeFileToArray($fullPath);
} catch (InvalidJsonException $exception) {
ErrorHandler::handleException($exception);

Expand Down
99 changes: 68 additions & 31 deletions backend/class/Context.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core;

use JetBrains\PhpStorm\Pure;
use noxkiwi\core\Constants\Mvc;
use noxkiwi\core\Exception\ContextException;
use noxkiwi\core\Exception\InvalidArgumentException;
use noxkiwi\core\Exception\SystemComponentException;
use noxkiwi\core\Helper\FrontendHelper;
use noxkiwi\core\Interfaces\ContextInterface;
Expand All @@ -15,10 +15,14 @@
use noxkiwi\translator\Traits\TranslatorTrait;
use function class_exists;
use function extension_loaded;
use function is_subclass_of;
use function method_exists;
use function newrelic_name_transaction;
use function strtoupper;
use function ucfirst;
use const E_ERROR;
use const E_USER_NOTICE;
use const E_WARNING;

/**
* I am the base Context class.
Expand Down Expand Up @@ -77,13 +81,18 @@ protected function __construct()
* @param string $contextName
*
* @throws \noxkiwi\core\Exception\SystemComponentException The desired context is not available.
* @throws \noxkiwi\core\Exception\SystemComponentException The desired class is not a descendant of the Context class.
* @throws \noxkiwi\singleton\Exception\SingletonException Obviously, if singleton fails.
* @return \noxkiwi\core\Context
*/
public static function get(string $contextName): Context
{
if (! class_exists($contextName)) {
throw new SystemComponentException('CONTEXT_NOT_AVAILABLE', E_ERROR, $contextName);
}
if (! is_subclass_of($contextName, self::class)) {
throw new SystemComponentException('CLASS_NOT_A_CONTEXT', E_ERROR, $contextName);
}

return $contextName::getInstance();
}
Expand All @@ -106,6 +115,7 @@ public function isAllowed(): bool
public function dispatch(Request $request): void
{
try {
$this->validate($request);
$response = $this->backendController($request);
} catch (\Exception $exception) {
ErrorHandler::handleException($exception);
Expand All @@ -119,14 +129,55 @@ public function dispatch(Request $request): void
}
}

/**
* I will validate the given Request against the configured parameter structure of the action and view.
*
* @param \noxkiwi\core\Request $request
*
* @throws \noxkiwi\core\Exception\InvalidArgumentException
* @return void
*/
protected function validate(Request $request): void
{
$actionConst = static::class . '::' . strtoupper("REQUEST_{$request->get(Mvc::ACTION)}_ACTION");
$errors = [];
if (defined($actionConst)) {
$design = constant($actionConst);
foreach ($design as $name => $validator) {
$val = $validator::get($validator);
$error = $val->validate($request->get($name));
if (empty($error)) {
continue;
}
$errors[$name] = $error;
}
}
$viewConst = static::class . '::' . strtoupper("REQUEST_{$request->get(Mvc::VIEW)}_VIEW");
if (defined($viewConst)) {
$design = constant($viewConst);
foreach ($design as $name => $validator) {
$val = $validator::get($validator);
$error = $val->validate($request->get($name));
if (empty($error)) {
continue;
}
$errors[$name] = $error;
}
}
if (empty($errors)) {
return;
}
throw new InvalidArgumentException('REQUEST_PARAMETERS_VIOLATED', E_WARNING, $errors);
}

/**
* I will perform any actions that belong to the backend of the application.
*
* @param \noxkiwi\core\Request $request
*
* @return \noxkiwi\core\Response
*/
final protected function backendController(Request $request): Response
protected function backendController(Request $request): Response
{
$this->doAction($request);
$this->doView($request);
Expand All @@ -145,11 +196,15 @@ final protected function doAction(Request $request): void
if ($action === null) {
return;
}
$action = static::makeActionMethod($action);
$action = 'action' . ucfirst($action);
if (! method_exists($this, $action)) {
return;
}
$this->$action();
try {
$this->$action();
} catch (Exception $exception) {
ErrorHandler::handleException($exception, E_USER_NOTICE);
}
}

/**
Expand All @@ -159,22 +214,16 @@ final protected function doAction(Request $request): void
*/
final protected function doView(Request $request): void
{
$method = static::getViewName($request->get(Mvc::VIEW, ''));
if (method_exists($this, $method)) {
$this->{$method}();
$view = $request->get(Mvc::VIEW, '');
$method = Mvc::VIEW . ucfirst($view);
if (! method_exists($this, $method)) {
return;
}
try {
$this->$method();
} catch (Exception $exception) {
ErrorHandler::handleException($exception, E_USER_NOTICE);
}
}

/**
* I will solely return the given $view's method name.
*
* @param string $view
*
* @return string
*/
#[Pure] final protected static function getViewName(string $view): string
{
return Mvc::VIEW . ucfirst($view);
}

/**
Expand Down Expand Up @@ -216,16 +265,4 @@ final protected function doOutput(Response $response): void
{
$response->pushOutput();
}

/**
* I will solely return the given $action's method name.
*
* @param string $action
*
* @return string
*/
#[Pure] private static function makeActionMethod(string $action): string
{
return 'action' . ucfirst($action);
}
}
2 changes: 1 addition & 1 deletion backend/class/Context/ResourceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class ResourceContext extends Context
* I will output the content of the requested file, and then I will exit.
* @throws \noxkiwi\singleton\Exception\SingletonException
*/
#[NoReturn] protected function viewFile(): void
#[NoReturn] final protected function viewFile(): void
{
[$type, $file] = explode(
'/',
Expand Down
3 changes: 1 addition & 2 deletions backend/class/Cookie/CookieCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function set(string $key, mixed $data): void
}
setcookie($key, $data, $this->getExpires(), '/', $_SERVER['HTTP_HOST'], false, true);
$_COOKIE[$key] = $data;
if ($this->get($key) === null) {
if ($this->get($key) !== $data) {
throw new CookieException('EXCEPTION_COOKIES_NOT_WRITABLE', E_NOTICE);
}
}
Expand All @@ -109,6 +109,5 @@ public function exists(string $key): bool
*/
public function put(array $data): void
{

}
}
2 changes: 1 addition & 1 deletion backend/class/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function initialize(): void
parent::initialize();
$data = JsonHelper::decodeFileToArray(self::getPath());
if (empty($data)) {
throw new ConfigurationException('ENVIRONMENT_INVALID', E_ERROR, ['path'=>self::getPath()]);
throw new ConfigurationException('ENVIRONMENT_INVALID', E_ERROR);
}
$this->add($data);
self::$loaded = true;
Expand Down
2 changes: 1 addition & 1 deletion backend/class/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Error extends \Error
* @param int $code
* @param \Throwable|null $previous
*/
#[Pure] public function __construct($message = '', $code = 0, Throwable $previous = null)
#[Pure] public function __construct(string $message = '', int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->detail = [];
Expand Down
2 changes: 2 additions & 0 deletions backend/class/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
abstract class ErrorHandler
{
private const SURROUND_LINES = 10;
public static int $errorsReported = 0;

/**
* I will handle the given $error as info element on a new Exception that will be handled right after.
Expand Down Expand Up @@ -67,6 +68,7 @@ final public static function handleError(\Error $error, int $errorLevel = E_ERRO
*/
public static function handleException(\Exception $exception, int $errorLevel = E_ERROR): void
{
self::$errorsReported++;
if (error_reporting() === 0) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion backend/class/ErrorStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use function count;

/**
* I am
* I am...
* @deprecated
* Class StackOf
* @package noxkiwi\core
*/
Expand Down
2 changes: 0 additions & 2 deletions backend/class/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
namespace noxkiwi\core;

use noxkiwi\hook\Hook;
use function chr;
use function file_put_contents;

/**
* I am the Core project's base exception.
Expand Down
8 changes: 8 additions & 0 deletions backend/class/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
*/
abstract class Gate extends Singleton implements GateInterface
{
/**
* @inheritDoc
*/
public function isOpen(): bool
{
return true;
}

/**
* @inheritDoc
*/
Expand Down
3 changes: 3 additions & 0 deletions backend/class/Gate/CidrGate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function setRanges(array $ranges): void
*/
#[Pure] public function isOpen(): bool
{
if (! parent::isOpen()) {
return false;
}
foreach ($this->allowedRanges as $allowedRange) {
if (WebHelper::isCidr($allowedRange) === true) {
return true;
Expand Down
7 changes: 6 additions & 1 deletion backend/class/Gate/CliGate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core\Gate;

use JetBrains\PhpStorm\Pure;
use noxkiwi\core\Gate;
use const PHP_SAPI;

Expand All @@ -19,8 +20,12 @@ final class CliGate extends Gate
/**
* @inheritDoc
*/
public function isOpen(): bool
#[Pure] public function isOpen(): bool
{
if (! parent::isOpen()) {
return false;
}

return PHP_SAPI === 'cli';
}
}
4 changes: 3 additions & 1 deletion backend/class/Gate/ClosedGate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core\Gate;

use JetBrains\PhpStorm\Pure;
use noxkiwi\core\Gate;

/**
Expand All @@ -18,8 +19,9 @@ final class ClosedGate extends Gate
{
/**
* @inheritDoc
* @noinspection PhpMissingParentCallCommonInspection
*/
public function isOpen(): bool
#[Pure] public function isOpen(): bool
{
return false;
}
Expand Down
Loading

0 comments on commit c9fcad3

Please sign in to comment.