Skip to content

Commit

Permalink
moved from BitBucket to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Nox committed Nov 10, 2021
1 parent 92c3f27 commit 499239e
Show file tree
Hide file tree
Showing 116 changed files with 7,537 additions and 674 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/sonarlint
674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

171 changes: 171 additions & 0 deletions backend/class/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core;

use JetBrains\PhpStorm\NoReturn;
use noxkiwi\core\Constants\Mvc;
use noxkiwi\core\Exception\ConfigurationException;
use noxkiwi\core\Gate\MaintenanceGate;
use noxkiwi\core\Helper\FrontendHelper;
use noxkiwi\core\Helper\LinkHelper;
use noxkiwi\core\Helper\WebHelper;
use noxkiwi\core\Interfaces\AppInterface;
use noxkiwi\core\Response\HttpResponse;
use noxkiwi\core\Traits\LanguageImprovementTrait;
use noxkiwi\hook\Hook;
use noxkiwi\log\Traits\LogTrait;
use noxkiwi\singleton\Singleton;
use ReflectionClass;
use function ucfirst;
use const E_ERROR;

/**
* I am the "class" everyone wants to have but won't ever have.
* - pun intended
*
* @package noxkiwi\core
* @author Jan Nox <[email protected]>
* @license https://nox.kiwi/license
* @copyright 2016 - 2021 noxkiwi
* @version 1.0.15
* @link https://nox.kiwi/
*/
abstract class App extends Singleton implements AppInterface
{
use LogTrait;
use LanguageImprovementTrait;

/** @var string I am the called App's vendor. */
private static string $vendor;
/** @var string I am the called App's name. */
private static string $app;

/**
* I will initialize the App class.
* This step consists of loading an environment and loading an Application instance.
* Also, I will check the maintenance status.
* @throws \noxkiwi\singleton\Exception\SingletonException
*/
protected function initialize(): void
{
parent::initialize();
$namespaceName = (new ReflectionClass($this))->getNamespaceName();
self::$app = explode('\\', $namespaceName)[1];
self::$vendor = explode('\\', $namespaceName)[0];
$this->checkMaintenance();
Hook::run('APP_INITIALIZING');
try {
Environment::getInstance();
Application::getInstance();
} catch (\Exception $exception) {
MaintenanceGate::getInstance()->close($exception->getCode());
ErrorHandler::handleException(new ConfigurationException('APP_INIT_FAIL', E_ERROR, $exception));
}
Hook::run('APP_INITIALIZED');
}

/**
* I will check the maintenance mode and stop the app if needed.
*/
final protected function checkMaintenance(): void
{
try {
if (MaintenanceGate::getInstance()->isOpen()) {
return;
}
} catch (\Exception $exception) {
ErrorHandler::handleException($exception);
}
Hook::run('APP_CONSTRUCT_MAINTENANCEMODE');
FrontendHelper::outputExit(MaintenanceGate::MAINTENANCE_TEMPLATE, HttpResponse::HEADER_ERROR, WebHelper::HTTP_SERVER_ERROR);
}

/**
* @inheritDoc
* @throws \noxkiwi\singleton\Exception\SingletonException
*/
#[NoReturn] public function run(): void
{
$request = Request::getInstance();
Hook::run('APP_RUN_START');
$request->set(Mvc::TEMPLATE, self::getTemplate());
$this->logDebug(__METHOD__, $request->get());
$contextName = static::getVendor();
$contextName .= "\\{$this->returnIt(static::getApp())}";
$contextName .= "\\Context";
$contextName .= '\\' . ucfirst($request->get(Mvc::CONTEXT, '')) . 'Context';
try {
$contextInstance = Context::get($contextName);
if (! $contextInstance->isAllowed()) {
Hook::run('APP_RUN_FORBIDDEN');
LinkHelper::forward([Mvc::CONTEXT => 'login', Mvc::VIEW => 'login']);
}
Hook::run('APP_RUN_ALLOWED');
$contextInstance->dispatch($request);
Hook::run('APP_RUN_END');
} catch (\Exception $exception) {
MaintenanceGate::getInstance()->close($exception->getCode());
ErrorHandler::handleException($exception);
exit(WebHelper::HTTP_SERVER_ERROR);
}
exit(WebHelper::HTTP_OKAY);
}

/**
* I return the vendor of this App
*
* @return string
*/
final public static function getVendor(): string
{
if (empty(self::$vendor)) {
return '';
}

return self::$vendor;
}

/**
* Simply returns the App's name
*
* @return string
*/
final public static function getApp(): string
{
if (empty(self::$app)) {
return '';
}

return self::$app;
}

/**
* I will return the template that will be used for displaying the page.
* It works by checking:
* -> Has a template been requested through the Request?
* -> Has the called $context/$view been configured to use a template?
* -> Has the called $app been configured to use a template?
* -> Otherwise return 'blank' template.
* @throws \noxkiwi\singleton\Exception\SingletonException
* @return string
*/
private static function getTemplate(): string
{
$request = Request::getInstance();
$template = $request->get(Mvc::TEMPLATE);
if (is_string($template)) {
return $template;
}
$context = $request->get(Mvc::CONTEXT);
$view = $request->get(Mvc::VIEW);
$template = (string)Application::getInstance()->get("context>$context>view>$view>template", '');
if (! empty($template)) {
return $template;
}
$template = (string)Application::getInstance()->get("context>$context>template", '');
if (! empty($template)) {
return $template;
}

return Application::getInstance()->get('defaulttemplate', 'blank');
}
}
135 changes: 135 additions & 0 deletions backend/class/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core;

use noxkiwi\cache\Cache;
use noxkiwi\core\Config\JsonConfig;
use noxkiwi\core\Constants\Mvc;
use noxkiwi\core\Exception\ConfigurationException;
use noxkiwi\core\Helper\ArrayHelper;
use noxkiwi\core\Traits\DatacontainerTrait;
use noxkiwi\validator\Validator\Structure\Config\AppValidator;
use noxkiwi\validator\Validator\Structure\Config\ContextValidator;
use function is_array;
use noxkiwi\singleton\Singleton;

/**
* I am the "class" everyone wants to have but won't ever have.
* - pun intended
*
* @package noxkiwi\core
* @author Jan Nox <[email protected]>
* @license https://nox.kiwi/license
* @copyright 2019 noxkiwi
* @version 1.0.0
* @link https://nox.kiwi/
*/
final class Application extends Singleton
{
use DatacontainerTrait;

private const CONFIG_DEFAULT_VIEW = 'defaultview';

/**
* I will construct the Application object.
* @throws \noxkiwi\core\Exception
* @throws \noxkiwi\core\Exception\ConfigurationException
* @throws \noxkiwi\core\Exception\InvalidArgumentException
*/
protected function __construct()
{
parent::__construct();
$this->init();
}

/**
* I will initialize the Application class.
* @throws \noxkiwi\core\Exception
* @throws \noxkiwi\core\Exception\ConfigurationException
* @throws \noxkiwi\core\Exception\InvalidArgumentException
*/
private function init(): void
{
$cachedConfig = Cache::getInstance()->get(Cache::DEFAULT_PREFIX, '_CONFIG_APP');
if (! empty($cachedConfig) && is_array($cachedConfig)) {
$this->add($cachedConfig);

return;
}
try {
$appConfig = (new JsonConfig(Path::CONFIG_APPLICATION))->get();
} catch (\Exception $exception) {
ErrorHandler::handleException($exception);
throw new ConfigurationException('app.json is invalid JSON', E_ERROR, 'INVALID_JSON');
}
if (! isset($appConfig[Mvc::CONTEXT])) {
throw new ConfigurationException('EXCEPTION_GETCONFIG_APPCONFIGCONTAINSNOCONTEXT', E_ERROR, $appConfig);
}
$default = (new JsonConfig(Path::getHomeDir() . Path::CONFIG_APPLICATION, true))->get();
$appConfig = ArrayHelper::arrayMergeRecursive($appConfig, $default);
foreach ($appConfig[Mvc::CONTEXT] as $contextName => $contextData) {
if (! isset($contextData['type'])) {
continue;
}
try {
$contextType = (new JsonConfig(Path::CONFIG_CONTEXT_DIR . '/' . $appConfig[Mvc::CONTEXT][$contextName]['type'] . '.json'))->get();
} catch (\Exception $exception) {
ErrorHandler::handleException($exception);
throw new ConfigurationException('EXCEPTION_GETCONFIG_APPCONFIGCONTEXTTYPEINVALID', E_ERROR, 'INVALID_JSON');
}
$errors = ContextValidator::getInstance()->validate($contextType);
if (! empty($errors)) {
throw new ConfigurationException('EXCEPTION_GETCONFIG_APPCONFIGCONTEXTTYPEINVALID', E_ERROR, $errors);
}
$appConfig[Mvc::CONTEXT][$contextName] = ArrayHelper::arrayMergeRecursive($appConfig[Mvc::CONTEXT][$contextName], $contextType);
if (is_array($appConfig[Mvc::CONTEXT][$contextName][self::CONFIG_DEFAULT_VIEW]) > 1) {
$appConfig[Mvc::CONTEXT][$contextName][self::CONFIG_DEFAULT_VIEW] = $appConfig[Mvc::CONTEXT][$contextName][self::CONFIG_DEFAULT_VIEW][0];
}
}
$errors = AppValidator::getInstance()->validate($appConfig);
if (! empty($errors)) {
throw new ConfigurationException('EXCEPTION_GETCONFIG_APPCONFIGFILEINVALID', E_ERROR, $errors);
}
Cache::getInstance()->set(Cache::DEFAULT_PREFIX, '_CONFIG_APP', $appConfig);
$this->add($appConfig);
}

/**
* I will return true if the given $Context exists in the current App.
*
* @param string|null $context
*
* @return bool
*/
public function contextExists(?string $context = null): bool
{
$context ??= Request::getInstance()->get(Mvc::CONTEXT);
try {
return self::getInstance()->exists("context>$context");
} catch (\Exception $exception) {
ErrorHandler::handleException($exception);
}

return false;
}

/**
* I will return true if the $view exists in the $Context of the current App.
*
* @param string|null $context
* @param string|null $view
*
* @return bool
*/
public function viewExists(?string $context = null, ?string $view = null): bool
{
$context ??= Request::getInstance()->get(Mvc::CONTEXT);
$view ??= Request::getInstance()->get(Mvc::VIEW);
try {
return $this->exists("context>$context>view>$view");
} catch (\Exception $exception) {
ErrorHandler::handleException($exception);
}

return false;
}
}
20 changes: 20 additions & 0 deletions backend/class/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core;

use noxkiwi\core\Interfaces\AuthInterface;
use noxkiwi\singleton\Singleton;

/**
* I am the base Auth class.
*
* @package noxkiwi\core
* @author Jan Nox <[email protected]>
* @license https://nox.kiwi/license
* @copyright 2016 - 2021 noxkiwi
* @version 1.0.2
* @link https://nox.kiwi/
*/
abstract class Auth extends Singleton implements AuthInterface
{
protected const USE_DRIVER = true;
}
19 changes: 19 additions & 0 deletions backend/class/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);
namespace noxkiwi\core;

use noxkiwi\core\Traits\DatacontainerTrait;

/**
* I am
*
* @package noxkiwi\core
* @author Jan Nox <[email protected]>
* @license https://nox.kiwi/license
* @copyright 2016 - 2018 noxkiwi
* @version 1.0.0
* @link https://nox.kiwi/
*/
class Config
{
use DatacontainerTrait;
}
Loading

0 comments on commit 499239e

Please sign in to comment.