dropkicked phpunit out, no longer expose Application to Twig and also defines.

This commit is contained in:
flash 2018-09-16 21:45:40 +02:00
parent a3fb32f65a
commit 1fe9a4fd92
13 changed files with 120 additions and 1615 deletions

1
.gitignore vendored
View file

@ -6,6 +6,7 @@
# Configuration # Configuration
/config/config.ini /config/config.ini
/.debug
# Storage # Storage
/store /store

View file

@ -18,9 +18,6 @@
"twig/extensions": "^1.5", "twig/extensions": "^1.5",
"filp/whoops": "^2.2" "filp/whoops": "^2.2"
}, },
"require-dev": {
"phpunit/phpunit": "~6.0"
},
"autoload": { "autoload": {
"classmap": [ "classmap": [
"database" "database"

1571
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,12 @@
<?php <?php
namespace Misuzu; namespace Misuzu;
define('MSZ_STARTUP', microtime(true));
define('MSZ_DEBUG', file_exists(__DIR__ . '/.debug'));
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
mb_internal_encoding('UTF-8'); mb_internal_encoding('UTF-8');
define('MSZ_DEBUG', file_exists(__DIR__ . '/vendor/phpunit/phpunit/composer.json'));
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
if (MSZ_DEBUG) { if (MSZ_DEBUG) {
@ -41,10 +42,7 @@ require_once __DIR__ . '/src/Users/session.php';
require_once __DIR__ . '/src/Users/user.php'; require_once __DIR__ . '/src/Users/user.php';
require_once __DIR__ . '/src/Users/validation.php'; require_once __DIR__ . '/src/Users/validation.php';
$app = new Application( $app = new Application(__DIR__ . '/config/config.ini');
__DIR__ . '/config/config.ini',
MSZ_DEBUG
);
$app->startDatabase(); $app->startDatabase();
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
@ -204,7 +202,7 @@ MIG;
} }
} }
} else { } else {
if (!$app->inDebugMode()) { if (!MSZ_DEBUG) {
ob_start('ob_gzhandler'); ob_start('ob_gzhandler');
} }
@ -217,7 +215,37 @@ MIG;
} }
$app->startCache(); $app->startCache();
$app->startTemplating();
tpl_init(['debug' => MSZ_DEBUG]);
tpl_var('globals', $app->getSiteInfo());
tpl_add_function('json_decode', true);
tpl_add_function('byte_symbol', true);
tpl_add_function('html_link', true);
tpl_add_function('html_colour', true);
tpl_add_function('url_construct', true);
tpl_add_function('country_name', true, 'get_country_name');
tpl_add_function('flip', true, 'array_flip');
tpl_add_function('first_paragraph', true);
tpl_add_function('colour_get_css', true);
tpl_add_function('colour_get_css_contrast', true);
tpl_add_function('colour_get_inherit', true);
tpl_add_function('colour_get_red', true);
tpl_add_function('colour_get_green', true);
tpl_add_function('colour_get_blue', true);
tpl_add_function('parse_line', true);
tpl_add_function('parse_text', true);
tpl_add_function('asset_url', true);
tpl_add_function('vsprintf', true);
tpl_add_function('perms_check', true);
tpl_add_function('git_commit_hash');
tpl_add_function('git_branch');
tpl_add_function('csrf_token', false, 'tmp_csrf_token');
tpl_add_function('startup_time', false, function (float $time = MSZ_STARTUP) {
return microtime(true) - $time;
});
tpl_add_path(__DIR__ . '/templates'); tpl_add_path(__DIR__ . '/templates');

View file

@ -67,7 +67,7 @@ if (!array_key_exists($settingsMode, $settingsModes)) {
$settingsErrors = []; $settingsErrors = [];
$disableAccountOptions = !$app->inDebugMode() && $app->disableRegistration(); $disableAccountOptions = !MSZ_DEBUG && $app->disableRegistration();
$avatarFileName = "{$app->getUserId()}.msz"; $avatarFileName = "{$app->getUserId()}.msz";
$avatarProps = $app->getAvatarProps(); $avatarProps = $app->getAvatarProps();
$backgroundProps = $app->getBackgroundProps(); $backgroundProps = $app->getBackgroundProps();

View file

@ -19,12 +19,6 @@ final class Application
{ {
private static $instance = null; private static $instance = null;
/**
* Whether the application is in debug mode, this should only be set in the constructor and never altered.
* @var bool
*/
private $debugMode = false;
/** /**
* Array of database connection names, first in the list is assumed to be the default. * Array of database connection names, first in the list is assumed to be the default.
*/ */
@ -52,32 +46,22 @@ final class Application
private $config = []; private $config = [];
/**
* TemplatingEngine instance.
* @var \Misuzu\TemplateEngine
*/
private $templatingInstance = null;
private $mailerInstance = null; private $mailerInstance = null;
private $geoipInstance = null; private $geoipInstance = null;
private $startupTime = 0;
/** /**
* Constructor, called by ApplicationBase::start() which also passes the arguments through. * Constructor, called by ApplicationBase::start() which also passes the arguments through.
* @param null|string $configFile * @param null|string $configFile
* @param bool $debug * @param bool $debug
*/ */
public function __construct(?string $configFile = null, bool $debug = false) public function __construct(?string $configFile = null)
{ {
if (!empty(self::$instance)) { if (!empty(self::$instance)) {
throw new UnexpectedValueException('An Application has already been set up.'); throw new UnexpectedValueException('An Application has already been set up.');
} }
self::$instance = $this; self::$instance = $this;
$this->startupTime = microtime(true);
$this->debugMode = $debug;
$this->config = is_file($configFile) ? parse_ini_file($configFile, true, INI_SCANNER_TYPED) : []; $this->config = is_file($configFile) ? parse_ini_file($configFile, true, INI_SCANNER_TYPED) : [];
if ($this->config === false) { if ($this->config === false) {
@ -85,7 +69,7 @@ final class Application
} }
// only use this error handler in prod mode, dev uses Whoops now // only use this error handler in prod mode, dev uses Whoops now
if (!$debug) { if (!MSZ_DEBUG) {
ExceptionHandler::register( ExceptionHandler::register(
false, false,
$this->config['Exceptions']['report_url'] ?? null, $this->config['Exceptions']['report_url'] ?? null,
@ -94,20 +78,6 @@ final class Application
} }
} }
public function getTimeSinceStart(): float
{
return microtime(true) - $this->startupTime;
}
/**
* Gets whether we're in debug mode or not.
* @return bool
*/
public function inDebugMode(): bool
{
return $this->debugMode;
}
/** /**
* Gets a storage path. * Gets a storage path.
* @param string $path * @param string $path
@ -224,53 +194,6 @@ final class Application
); );
} }
/**
* Sets up the templating engine module.
*/
public function startTemplating(): void
{
if (!is_null($this->templatingInstance)) {
throw new UnexpectedValueException('Templating module has already been started.');
}
tpl_init([
'debug' => $this->debugMode,
]);
tpl_var('globals', [
'site_name' => $this->config['Site']['name'] ?? 'Flashii',
'site_description' => $this->config['Site']['description'] ?? '',
'site_twitter' => $this->config['Site']['twitter'] ?? '',
'site_url' => $this->config['Site']['url'] ?? '',
]);
tpl_add_function('json_decode', true);
tpl_add_function('byte_symbol', true);
tpl_add_function('html_link', true);
tpl_add_function('html_colour', true);
tpl_add_function('url_construct', true);
tpl_add_function('country_name', true, 'get_country_name');
tpl_add_function('flip', true, 'array_flip');
tpl_add_function('first_paragraph', true);
tpl_add_function('colour_get_css', true);
tpl_add_function('colour_get_css_contrast', true);
tpl_add_function('colour_get_inherit', true);
tpl_add_function('colour_get_red', true);
tpl_add_function('colour_get_green', true);
tpl_add_function('colour_get_blue', true);
tpl_add_function('parse_line', true);
tpl_add_function('parse_text', true);
tpl_add_function('asset_url', true);
tpl_add_function('vsprintf', true);
tpl_add_function('perms_check', true);
tpl_add_function('git_commit_hash');
tpl_add_function('git_branch');
tpl_add_function('csrf_token', false, 'tmp_csrf_token');
tpl_var('app', $this);
}
public function startMailer(): void public function startMailer(): void
{ {
if (!empty($this->mailerInstance)) { if (!empty($this->mailerInstance)) {
@ -403,6 +326,16 @@ final class Application
]; ];
} }
public function getSiteInfo(): array
{
return [
'site_name' => $this->config['Site']['name'] ?? 'Flashii',
'site_description' => $this->config['Site']['description'] ?? '',
'site_twitter' => $this->config['Site']['twitter'] ?? '',
'site_url' => $this->config['Site']['url'] ?? '',
];
}
public function getDefaultAvatar(): string public function getDefaultAvatar(): string
{ {
return $this->getPath($this->config['Avatar']['default_path'] ?? 'public/images/no-avatar.png'); return $this->getPath($this->config['Avatar']['default_path'] ?? 'public/images/no-avatar.png');

View file

@ -76,5 +76,9 @@ function tpl_render(string $path, array $vars = []): string
tpl_vars($vars); tpl_vars($vars);
} }
if (!defined('MSZ_TPL_RENDER')) {
define('MSZ_TPL_RENDER', microtime(true));
}
return $twig->render($path, $GLOBALS[MSZ_TPL_VARS_STORE]); return $twig->render($path, $GLOBALS[MSZ_TPL_VARS_STORE]);
} }

View file

@ -7,7 +7,7 @@
{{ globals.site_name }} {{ globals.site_name }}
</a> </a>
<label style="background-image:url('/profile.php?u={{ app.hasActiveSession ? current_user.user_id : 0 }}&amp;m=avatar');" <label style="background-image:url('/profile.php?u={{ current_user.user_id|default(0) }}&amp;m=avatar');"
class="avatar header__icon header__icon--user" class="avatar header__icon header__icon--user"
for="toggle-mobile-header-user"></label> for="toggle-mobile-header-user"></label>
</div> </div>
@ -38,7 +38,7 @@
<input type="checkbox" class="header__user-toggle" id="toggle-mobile-header-user"> <input type="checkbox" class="header__user-toggle" id="toggle-mobile-header-user">
<div class="header__user"> <div class="header__user">
{% if app.hasActiveSession %} {% if current_user is defined %}
<a href="/profile.php?u={{ current_user.user_id }}" <a href="/profile.php?u={{ current_user.user_id }}"
class="avatar header__user__avatar" class="avatar header__user__avatar"
style="background-image:url('/profile.php?u={{ current_user.user_id }}&amp;m=avatar');"></a> style="background-image:url('/profile.php?u={{ current_user.user_id }}&amp;m=avatar');"></a>
@ -49,7 +49,7 @@
{% endif %} {% endif %}
<div class="header__user__info"> <div class="header__user__info">
{% if app.hasActiveSession %} {% if current_user is defined %}
<a href="/profile.php?u={{ current_user.user_id }}" <a href="/profile.php?u={{ current_user.user_id }}"
class="header__user__name" class="header__user__name"
style="{{ current_user.user_colour|html_colour({ style="{{ current_user.user_colour|html_colour({
@ -66,7 +66,7 @@
{% endif %} {% endif %}
<ul class="header__user__actions"> <ul class="header__user__actions">
{% if app.hasActiveSession %} {% if current_user is defined %}
<li class="header__user__action"> <li class="header__user__action">
<a class="header__user__link" href="/profile.php?u={{ current_user.user_id }}">Profile</a> <a class="header__user__link" href="/profile.php?u={{ current_user.user_id }}">Profile</a>
</li> </li>

View file

@ -16,7 +16,7 @@
{% endif %} {% endif %}
{% if forum_info.forum_type == 0 %} {% if forum_info.forum_type == 0 %}
{% set fcbuttons = app.hasActiveSession ? forum_category_buttons(forum_info, forum_perms) : '' %} {% set fcbuttons = current_user is defined ? forum_category_buttons(forum_info, forum_perms) : '' %}
{% set fcpagination = pagination( {% set fcpagination = pagination(
forum_info.forum_topic_count, forum_info.forum_topic_count,
forum_range, forum_range,

View file

@ -15,7 +15,7 @@
'o': topic_offset, 'o': topic_offset,
}) %} }) %}
{% set can_reply = app.hasActiveSession and topic_info.topic_locked is null and not topic_info.topic_archived %} {% set can_reply = current_user is defined and topic_info.topic_locked is null and not topic_info.topic_archived %}
{% set ftbuttons = can_reply ? forum_topic_buttons(topic_info) : '' %} {% set ftbuttons = can_reply ? forum_topic_buttons(topic_info) : '' %}
{% set ftpagination = pagination( {% set ftpagination = pagination(
topic_info.topic_post_count, topic_info.topic_post_count,

View file

@ -25,7 +25,7 @@
</div> </div>
<div class="index__main"> <div class="index__main">
{% if app.hasActiveSession %} {% if current_user is defined %}
<div class="container"> <div class="container">
<div class="container__title">Welcome</div> <div class="container__title">Welcome</div>
<div class="container__content"> <div class="container__content">

View file

@ -48,7 +48,8 @@
# {{ ('https://github.com/flashwave/misuzu/commit/' ~ git_commit_hash(true))|html_link(git_commit_hash(), 'footer__link') }} # {{ ('https://github.com/flashwave/misuzu/commit/' ~ git_commit_hash(true))|html_link(git_commit_hash(), 'footer__link') }}
{% if query_count is defined %} {% if query_count is defined %}
/ SQL Queries: {{ query_count|number_format }} / SQL Queries: {{ query_count|number_format }}
/ Took: {{ app.timeSinceStart|number_format(5) }} seconds / Took: {{ startup_time()|number_format(5) }} seconds
/ Render: {{ startup_time(constant('MSZ_TPL_RENDER'))|number_format(5) }} seconds
{% endif %} {% endif %}
</div> </div>
{% endautoescape %} {% endautoescape %}

View file

@ -167,7 +167,7 @@
{% endif %} {% endif %}
</div> </div>
{% if app.hasActiveSession %} {% if current_user is defined %}
{% spaceless %} {% spaceless %}
<div class="profile__info__section"> <div class="profile__info__section">
<div class="profile__info__block profile__info__block--links"> <div class="profile__info__block profile__info__block--links">