Optimized overhead a tiny bit

This commit is contained in:
Marcin Kurczewski 2014-08-23 14:15:04 +02:00
parent 29384a4b91
commit 46e47f6f39
11 changed files with 75 additions and 36 deletions

@ -1 +1 @@
Subproject commit ba4284ea7d1a43123e5be23afa62322932467125 Subproject commit eba4f6f486f540a4301f5b20fb68e14bb9b20466

View file

@ -6,9 +6,19 @@ class Access
public static function init() public static function init()
{ {
self::$privileges = [];
self::$checkPrivileges = true; self::$checkPrivileges = true;
self::$privileges = \Chibi\Cache::getCache('privileges', [get_called_class(), 'getPrivilegesFromConfig']);
}
public static function initWithoutCache()
{
self::$checkPrivileges = true;
self::$privileges = self::getPrivilegesFromConfig();
}
public static function getPrivilegesFromConfig()
{
$privileges = [];
foreach (Core::getConfig()->privileges as $key => $minAccessRankName) foreach (Core::getConfig()->privileges as $key => $minAccessRankName)
{ {
if (strpos($key, '.') === false) if (strpos($key, '.') === false)
@ -19,14 +29,15 @@ class Access
if (!in_array($privilegeName, Privilege::getAllConstants())) if (!in_array($privilegeName, Privilege::getAllConstants()))
throw new Exception('Invalid privilege name in config: ' . $privilegeName); throw new Exception('Invalid privilege name in config: ' . $privilegeName);
if (!isset(self::$privileges[$privilegeName])) if (!isset($privileges[$privilegeName]))
{ {
self::$privileges[$privilegeName] = []; $privileges[$privilegeName] = [];
self::$privileges[$privilegeName][null] = $minAccessRank; $privileges[$privilegeName][null] = $minAccessRank;
} }
self::$privileges[$privilegeName][$subPrivilegeName] = $minAccessRank; $privileges[$privilegeName][$subPrivilegeName] = $minAccessRank;
} }
return $privileges;
} }
public static function check(Privilege $privilege, $user = null) public static function check(Privilege $privilege, $user = null)

View file

@ -3,6 +3,12 @@ class Assets extends \Chibi\Util\Assets
{ {
private $pageThumbnail = null; private $pageThumbnail = null;
private $subTitle = null; private $subTitle = null;
private $engineVersion = null;
public function __construct()
{
$this->engineVersion = PropertyModel::get(PropertyModel::EngineVersion);
}
public function setSubTitle($text) public function setSubTitle($text)
{ {
@ -16,7 +22,7 @@ class Assets extends \Chibi\Util\Assets
public function addStylesheet($path) public function addStylesheet($path)
{ {
return $this->addStylesheetFullPath($this->decorateUrl('/media/css/' . $path)); return parent::addStylesheet('/media/css/' . $path . '?' . $this->engineVersion);
} }
public function addStylesheetFullPath($path) public function addStylesheetFullPath($path)
@ -26,7 +32,7 @@ class Assets extends \Chibi\Util\Assets
public function addScript($path) public function addScript($path)
{ {
return $this->addScriptFullPath($this->decorateUrl('/media/js/' . $path)); return $this->addScriptFullPath('/media/js/' . $path . '?' . $this->engineVersion);
} }
public function addScriptFullPath($path) public function addScriptFullPath($path)
@ -57,10 +63,4 @@ class Assets extends \Chibi\Util\Assets
$html = str_replace('</body>', $bodySnippet . '</body>', $html); $html = str_replace('</body>', $bodySnippet . '</body>', $html);
return $html; return $html;
} }
private function decorateUrl($url)
{
return $url . '?' . PropertyModel::get(PropertyModel::EngineVersion);
}
} }

View file

@ -199,9 +199,19 @@ class TextHelper
public static function reprTags($tags) public static function reprTags($tags)
{ {
if (empty($tags))
return '';
$x = []; $x = [];
if (is_object($tags[0]))
{
foreach ($tags as $tag) foreach ($tags as $tag)
$x []= self::reprTag($tag); $x []= '#' . $tag->getName();
}
else
{
foreach ($tags as $tag)
$x []= '#' . $tag;
}
natcasesort($x); natcasesort($x);
return join(', ', $x); return join(', ', $x);
} }

View file

@ -11,9 +11,6 @@ if (!empty(Core::getConfig()->appearance->extraScripts))
if (!empty(Core::getConfig()->appearance->extraStylesheets)) if (!empty(Core::getConfig()->appearance->extraStylesheets))
foreach (Core::getConfig()->appearance->extraStylesheets as $path) foreach (Core::getConfig()->appearance->extraStylesheets as $path)
$this->assets->addStylesheetFullPath($path); $this->assets->addStylesheetFullPath($path);
$lastSearchQuery = isset($this->context->transport->lastSearchQuery)
? $this->context->transport->lastSearchQuery
: '';
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
@ -29,6 +26,11 @@ $lastSearchQuery = isset($this->context->transport->lastSearchQuery)
</head> </head>
<body> <body>
<?php
$lastSearchQuery = isset($this->context->transport->lastSearchQuery)
? $this->context->transport->lastSearchQuery
: '';
?>
<div id="settings" <div id="settings"
data-last-search-query="<?= htmlspecialchars($lastSearchQuery) ?>" data-last-search-query="<?= htmlspecialchars($lastSearchQuery) ?>"
data-thumbnail-width="<?= Core::getConfig()->browsing->thumbnailWidth ?>" data-thumbnail-width="<?= Core::getConfig()->browsing->thumbnailWidth ?>"

View file

@ -25,7 +25,7 @@ final class Core
require_once self::$rootDir . 'lib' . DS . 'chibi-sql' . DS . 'include.php'; require_once self::$rootDir . 'lib' . DS . 'chibi-sql' . DS . 'include.php';
\Chibi\AutoLoader::registerFilesystem(__DIR__); \Chibi\AutoLoader::registerFilesystem(__DIR__);
self::$router = new Router(); self::$router = \Chibi\Cache::getCache('router', function() { return new Router(); });
self::prepareConfig(); self::prepareConfig();
self::checkExtensions(); self::checkExtensions();
} }

View file

@ -11,8 +11,7 @@ class ReflectionBasedTestRunner implements ITestRunner
public function run() public function run()
{ {
$testFixtures = $this->getTestFixtures($this->filter); $testFixtures = $this->getTestFixtures($this->filter);
$success = $this->runAll($testFixtures); return $this->runAll($testFixtures);
exit($success ? 0 : 1);
} }
public function setFilter($filter) public function setFilter($filter)

View file

@ -1,4 +1,7 @@
<?php <?php
require_once __DIR__ . '/../src/core.php';
\Chibi\Autoloader::registerFileSystem(__DIR__);
class SzurubooruTestRunner implements ITestRunner class SzurubooruTestRunner implements ITestRunner
{ {
public function run() public function run()
@ -40,7 +43,21 @@ class SzurubooruTestRunner implements ITestRunner
}); });
}); });
$testRunner->run(); $success = $testRunner->run();
$this->removeCache();
return $success;
}
private function removeCache()
{
$cachePath = __DIR__
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'lib'
. DIRECTORY_SEPARATOR . 'chibi-core'
. DIRECTORY_SEPARATOR . 'cache';
foreach (glob($cachePath . DIRECTORY_SEPARATOR . '*.dat') as $fn)
unlink($fn);
} }
private function printHelp() private function printHelp()
@ -157,7 +174,7 @@ class SzurubooruTestRunner implements ITestRunner
private function prepareTestConfig($options) private function prepareTestConfig($options)
{ {
$config = new \Chibi\Config(); $config = new \Chibi\Config();
$config->loadIni(Core::getConfig()->rootDir . DS . 'tests' . DS . 'config.ini'); $config->loadIni(Core::getConfig()->rootDir . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'config.ini');
$config->isForTests = true; $config->isForTests = true;
$config->main->dbDriver = $options->dbDriver; $config->main->dbDriver = $options->dbDriver;
@ -180,6 +197,7 @@ class SzurubooruTestRunner implements ITestRunner
$_SESSION = []; $_SESSION = [];
Auth::setCurrentUser(null); Auth::setCurrentUser(null);
$this->removeCache();
$this->removeTestFolders(); $this->removeTestFolders();
$this->prepareTestConfig($options); $this->prepareTestConfig($options);
Core::prepareEnvironment(); Core::prepareEnvironment();

View file

@ -34,12 +34,12 @@ class AbstractTest
protected function grantAccess($privilege) protected function grantAccess($privilege)
{ {
Core::getConfig()->privileges->$privilege = 'anonymous'; Core::getConfig()->privileges->$privilege = 'anonymous';
Access::init(); Access::initWithoutCache();
} }
protected function revokeAccess($privilege) protected function revokeAccess($privilege)
{ {
Core::getConfig()->privileges->$privilege = 'nobody'; Core::getConfig()->privileges->$privilege = 'nobody';
Access::init(); Access::initWithoutCache();
} }
} }

View file

@ -29,7 +29,7 @@ class AccessTest extends AbstractTest
public function testAccessRanks2() public function testAccessRanks2()
{ {
Core::getConfig()->privileges->listPosts = 'power-user'; Core::getConfig()->privileges->listPosts = 'power-user';
Access::init(); Access::initWithoutCache();
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::Admin)); $user->setAccessRank(new AccessRank(AccessRank::Admin));
@ -51,7 +51,7 @@ class AccessTest extends AbstractTest
public function testSubPrivilegesOnlySub() public function testSubPrivilegesOnlySub()
{ {
Core::getConfig()->privileges->{'listPosts.own'} = 'power-user'; Core::getConfig()->privileges->{'listPosts.own'} = 'power-user';
Access::init(); Access::initWithoutCache();
$user = $this->userMocker->mockSingle(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $user->setAccessRank(new AccessRank(AccessRank::PowerUser));
@ -64,7 +64,7 @@ class AccessTest extends AbstractTest
{ {
Core::getConfig()->privileges->{'listPosts.own'} = 'power-user'; Core::getConfig()->privileges->{'listPosts.own'} = 'power-user';
Core::getConfig()->privileges->{'listPosts'} = 'admin'; Core::getConfig()->privileges->{'listPosts'} = 'admin';
Access::init(); Access::initWithoutCache();
$this->testSubPrivilegesSubAndGeneral(); $this->testSubPrivilegesSubAndGeneral();
} }
@ -72,7 +72,7 @@ class AccessTest extends AbstractTest
{ {
Core::getConfig()->privileges->{'listPosts'} = 'admin'; Core::getConfig()->privileges->{'listPosts'} = 'admin';
Core::getConfig()->privileges->{'listPosts.own'} = 'power-user'; Core::getConfig()->privileges->{'listPosts.own'} = 'power-user';
Access::init(); Access::initWithoutCache();
$this->testSubPrivilegesSubAndGeneral(); $this->testSubPrivilegesSubAndGeneral();
} }
@ -95,7 +95,7 @@ class AccessTest extends AbstractTest
Core::getConfig()->privileges->{'listPosts.own'} = 'power-user'; Core::getConfig()->privileges->{'listPosts.own'} = 'power-user';
Core::getConfig()->privileges->{'listPosts.all'} = 'admin'; Core::getConfig()->privileges->{'listPosts.all'} = 'admin';
Core::getConfig()->privileges->{'listPosts'} = 'nobody'; Core::getConfig()->privileges->{'listPosts'} = 'nobody';
Access::init(); Access::initWithoutCache();
$this->testSubPrivilegesMultipleSubAndGeneral(); $this->testSubPrivilegesMultipleSubAndGeneral();
} }
@ -104,7 +104,7 @@ class AccessTest extends AbstractTest
Core::getConfig()->privileges->{'listPosts'} = 'nobody'; Core::getConfig()->privileges->{'listPosts'} = 'nobody';
Core::getConfig()->privileges->{'listPosts.own'} = 'power-user'; Core::getConfig()->privileges->{'listPosts.own'} = 'power-user';
Core::getConfig()->privileges->{'listPosts.all'} = 'admin'; Core::getConfig()->privileges->{'listPosts.all'} = 'admin';
Access::init(); Access::initWithoutCache();
$this->testSubPrivilegesMultipleSubAndGeneral(); $this->testSubPrivilegesMultipleSubAndGeneral();
} }

View file

@ -1,6 +1,5 @@
<?php <?php
require_once __DIR__ . '/../src/core.php'; require_once 'SzurubooruTestRunner.php';
\Chibi\Autoloader::registerFileSystem(__DIR__);
$runner = new SzurubooruTestRunner(); $runner = new SzurubooruTestRunner();
$runner->run(); $success = $runner->run();
exit($success ? 0 : 1);