Progress towards slim4/php-di etc

This commit is contained in:
Greyscale 2020-06-22 21:38:55 +02:00
parent a242c4e924
commit 336a5f41bf
7 changed files with 174 additions and 19 deletions

View file

@ -99,10 +99,12 @@ class App
(new ContainerBuilder())
->useAutowiring(true)
->useAnnotations(true)
//->enableCompilation(APP_ROOT . "/cache")
//->writeProxiesToFile(true, APP_ROOT . "/cache/injection-proxies")
->build()
;
if (file_exists('/app/cache')) {
// $container->enableCompilation("/app/cache");
// $container->writeProxiesToFile(true, "/app/cache/injection-proxies");
}
$container = $container->build();
$container->set(Slim\Views\Twig::class, function (ContainerInterface $container) {
foreach ($this->viewPaths as $i => $viewLocation) {
@ -110,7 +112,7 @@ class App
unset($this->viewPaths[$i]);
}
}
$settings = ['cache' => 'cache/twig'];
$settings = ['cache' => APP_ROOT.'/cache/twig'];
$loader = new FilesystemLoader();
foreach ($this->viewPaths as $path) {
@ -205,14 +207,14 @@ class App
return $monolog;
});
#$container->set(DebugBar::class, function (ContainerInterface $container) {
# $debugBar = new StandardDebugBar();
# /** @var Logger $logger */
# $logger = $container->get(Logger::class);
# $debugBar->addCollector(new MonologCollector($logger));
#
# return $debugBar;
# });
//$container->set(DebugBar::class, function (ContainerInterface $container) {
// $debugBar = new StandardDebugBar();
// /** @var Logger $logger */
// $logger = $container->get(Logger::class);
// $debugBar->addCollector(new MonologCollector($logger));
//
// return $debugBar;
// });
$container->set(\Middlewares\Debugbar::class, function (ContainerInterface $container) {
$debugBar = $container->get(DebugBar::class);

View file

@ -2,8 +2,8 @@
namespace Benzine\Controllers;
use Benzine\Exceptions\FilterDecodeException;
use Benzine\Controllers\Filters\Filter;
use Benzine\Exceptions\FilterDecodeException;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
@ -53,14 +53,17 @@ abstract class Controller
public function xmlResponse(\SimpleXMLElement $root, Request $request, Response $response): Response
{
$response = $response->withBody($root->asXML());
$response->getBody()->write($root->asXML());
return $response->withHeader('Content-type', 'text/xml');
}
public function jsonResponse($json, Request $request, Response $response): Response
{
return $response->withJson($json);
$content = json_encode($json, JSON_PRETTY_PRINT);
$response->getBody()->write($content);
return $response->withHeader('Content-type', 'application/json');
}
public function jsonResponseException(\Exception $e, Request $request, Response $response): Response

View file

@ -2,10 +2,10 @@
namespace Benzine\Middleware;
use Benzine\Router\Route;
use Faker\Factory;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Benzine\Router\Route;
class AccessRequirements
{

View file

@ -2,7 +2,6 @@
namespace Benzine\Middleware;
use Benzine\Configuration;
use Benzine\ORM\Profiler;
use Benzine\Services\ConfigurationService;
use Benzine\⌬;

View file

@ -86,6 +86,10 @@ class ConfigurationService
protected function setupDefines(): void
{
if(defined("APP_ROOT")){
return;
}
define('APP_ROOT', $this->appRoot);
define('APP_NAME', $this->get('application/name'));
define('APP_START', microtime(true));

View file

@ -2,12 +2,159 @@
namespace Benzine\Services;
class SessionService
use ⌬\Session\Session;
class SessionService implements \SessionHandlerInterface
{
protected \Redis $redis;
protected $oldID;
private int $lifetime = 43200;
private bool $sessionInitialised = false;
public function __construct(\Redis $redis)
{
$this->redis = $redis;
}
/**
* @return int
*/
public function getLifetime(): int
{
return $this->lifetime;
}
/**
* @param int $lifetime
*/
public function setLifetime(int $lifetime): void
{
$this->lifetime = $lifetime;
}
public function initSession()
{
if ($this->sessionInitialised) {
return;
}
// set how long server should keep session data
ini_set('session.gc_maxlifetime', $this->getLifetime());
// set how long each client should remember their session id
session_set_cookie_params($this->getLifetime());
session_set_save_handler($this, true);
// Prevent session from influencing the slim headers sent back to the browser.
session_cache_limiter(null);
// Begin the Session
session_start();
$this->sessionInitialised = true;
}
public function close()
{
return true;
}
public function destroy($session_id)
{
$this->oldID = $session_id;
return true;
}
public function gc($maxlifetime)
{
return true;
}
public function open($save_path, $name)
{
return true;
}
public function read($session_id)
{
if ($this->useAPCU()) {
if (apcu_exists('read-'.$session_id)) {
return apcu_fetch('read-'.$session_id);
}
}
if (!empty($this->oldID)) {
$session_id = $this->oldID ? $this->oldID : $session_id;
}
$serialised = $this->redis->get("session:{$session_id}");
if (null != $serialised) {
if (!empty($this->oldID)) {
// clean up old session after regenerate
$this->redis->del("session:{$session_id}");
$this->oldID = null;
}
$result = unserialize($serialised);
} else {
$result = null;
}
if ($this->useAPCU()) {
apcu_store('read-'.$session_id, $result, 30);
} else {
self::$dirtyCheck['read-'.$session_id] = crc32($result);
}
return $result;
}
public function write($session_id, $session_data)
{
$dirty = false;
if ($this->useAPCU()) {
$dirty = crc32(apcu_fetch('read-'.$session_id)) != crc32($data);
} else {
$dirty = self::$dirtyCheck['read-'.$session_id] != crc32($data);
}
if ($dirty) {
$this->redis->set("session:{$session_id}", serialize($data));
$this->redis->expire("session:{$session_id}", $this->keyLifeTime);
}
apcu_store('read-'.$session_id, $data);
return true;
}
public function get(string $key)
{
if (isset($_SESSION[$key])) {
return unserialize($_SESSION[$key]);
}
return null;
}
public function set(string $key, $value): bool
{
$_SESSION[$key] = serialize($value);
return true;
}
public function dispose(string $key): bool
{
if (isset($_SESSION[$key])) {
unset($_SESSION[$key]);
return true;
}
return false;
}
private function useAPCU(): bool
{
return function_exists('apcu_store');
}
}

View file

@ -129,7 +129,7 @@ abstract class AbstractQueueWorker extends AbstractWorker
foreach ($items as $item) {
$processResults = $this->process($item);
if (is_array($processResults)) {
foreach($processResults as $processResult) {
foreach ($processResults as $processResult) {
$resultItems[] = $processResult;
}
} else {