Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Update to use the new resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEssex committed Nov 5, 2023
1 parent 29817fa commit f2ab412
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 235 deletions.
312 changes: 167 additions & 145 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 1 addition & 11 deletions src/Context/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AppContext extends Context implements Translatable
protected function _initialize(): void
{
parent::_initialize();
$cubex = $this->getCubex();
$cubex = @$this->getCubex();

//Register the database
$databaseProvider = DatabaseProvider::instance($this);
Expand All @@ -40,14 +40,4 @@ public function linkBuilder(string $path = '', array $query = []): LinkBuilder
{
return LinkBuilder::fromRequest($this->request(), $path, $query)->setSubDomain('www');
}

public function flash(): FlashMessageProvider
{
return $this->getCubex()->retrieve(FlashMessageProvider::class);
}

public function seo(): SeoProvider
{
return $this->getCubex()->retrieve(SeoProvider::class);
}
}
1 change: 1 addition & 0 deletions src/Context/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ abstract class AbstractProvider implements ContextAware, WithContext
* Returns an instance of the provider
*
* @param Context $context
* @param mixed ...$args
*
* @return static
*/
Expand Down
27 changes: 25 additions & 2 deletions src/Http/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,37 @@

namespace CubexBase\Application\Http\Controllers;

use CubexBase\Application\Context\Providers\FlashMessageProvider;
use CubexBase\Application\Context\Providers\SeoProvider;
use CubexBase\Application\Http\Forms\ExampleForm\ExampleForm;
use CubexBase\Application\Http\Layout\LayoutController;
use CubexBase\Application\Http\Views\Home\HomeView;
use Packaged\Http\Responses\RedirectResponse;

class FrontendController extends LayoutController
{
protected function _generateRoutes()
{
yield self::_route('$', HomeController::class);
yield self::_route('test', TestController::class);
yield self::_route('$', 'home');
yield self::_route('test', 'test');
yield self::_route('secure', SecureController::class);
}

public function getHome(SeoProvider $seo)
{
$form = ExampleForm::withContext($this, 'example_form');
$form->hydrate($this->request()->request->all());
$form->hydrate($this->request()->query->all());

$seo->meta('description', 'This is the description');

return HomeView::withContext($this)
->setForm($form);
}

public function getTest(FlashMessageProvider $flash)
{
$flash->addMessage('success', 'This is a test message');
return RedirectResponse::create('/');
}
}
45 changes: 0 additions & 45 deletions src/Http/Controllers/HomeController.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Http/Controllers/TestController.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace CubexBase\Application\Http\Layout;
namespace CubexBase\Application\Http\Layout\DefaultLayout;

use CubexBase\Application\Context\AppContext;
use CubexBase\Application\Context\Providers\FlashMessageProvider;
use CubexBase\Application\Context\Providers\SeoProvider;
use CubexBase\Application\Http\Components\Footer\Footer;
use CubexBase\Application\Http\Components\Header\Header;
use Packaged\Context\ContextAware;
Expand All @@ -24,7 +26,10 @@ class Layout extends Element implements ContextAware, WithContext
protected mixed $_header;
protected mixed $_footer;

public function __construct()
public function __construct(
public ?SeoProvider $seoProvider = null,
public ?FlashMessageProvider $flash = null
)
{
// Require default resources
ResourceManager::resources()->requireCss('main.min.css');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use CubexBase\Application\Http\Layout\Layout;
use CubexBase\Application\Http\Layout\DefaultLayout\Layout;
use Packaged\Dispatch\Dispatch;
use Packaged\Dispatch\ResourceStore;

Expand All @@ -22,16 +22,16 @@ $store = Dispatch::instance()->store();
<link type="text/plain" rel="author" href="<?= $ctx->linkBuilder('/humans.txt') ?>">
<link rel="manifest" href="<?= $ctx->linkBuilder('/site.webmanifest') ?>">

<?php if($this->getContext()->seo()): ?>
<?= $this->getContext()->seo() ?>
<?php if($this->seoProvider): ?>
<?= $this->seoProvider ?>
<?php endif; ?>

<?= $store->generateHtmlIncludes() ?>
</head>

<body class="<?= $this->getPageClass() ?>">

<?php foreach($ctx->flash()->getMessages() as $message): ?>
<?php foreach($this->flash->getMessages() as $message): ?>
<div class="alert alert-<?= $message['type'] ?>">
<p><?= $message['message'] ?></p>
</div>
Expand Down
14 changes: 13 additions & 1 deletion src/Http/Layout/LayoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CubexBase\Application\Http\Layout;

use Cubex\CubexAware;
use CubexBase\Application\Http\Layout\DefaultLayout\Layout;
use CubexBase\Application\Http\Views\AbstractView;
use CubexBase\Application\MainApplication;
use Exception;
Expand Down Expand Up @@ -32,7 +34,7 @@ protected function _prepareResponse(Context $c, $result, $buffer = null)
}
}

$theme = Layout::withContext($this);
$theme = $this->getTheme();
$theme->setContent($result);

if($result instanceof AbstractView)
Expand Down Expand Up @@ -62,4 +64,14 @@ protected function _isAppropriateResponse($result): bool
{
return $result instanceof Element || $result instanceof HtmlElement || is_scalar($result) || is_array($result);
}

public function getTheme()
{
$ctx = $this->getContext();
if($ctx instanceof CubexAware)
{
return $ctx->getCubex()->resolve(Layout::class);
}
return Layout::withContext($this);
}
}
16 changes: 16 additions & 0 deletions src/Http/Layout/WithErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace CubexBase\Application\Http\Layout;

use Cubex\Controller\AuthedController;
use Cubex\Cubex;
use Cubex\CubexAware;
use Cubex\I18n\GetTranslatorTrait;
use CubexBase\Application\Context\AppContext;
use CubexBase\Application\Http\Middleware\WithMiddlewareTrait;
Expand All @@ -29,6 +31,20 @@ protected function _getHandler(Context $context): callable|string|Handler
return $handler ?: 'error';
}

protected function _processHandler(Context $c, $handler, &$response): bool
{
while(is_callable($handler))
{
/** @var Cubex $cubex */
if($c instanceof CubexAware)
{
$cubex = @$c->getCubex();
$handler = $cubex->resolveMethod($handler[0], $handler[1]);
}
}
return parent::_processHandler($c, $handler, $response);
}

protected function _makeCubexResponse($content): Response
{
$result = parent::_makeCubexResponse($content);
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Views/Home/HomeView.phtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use CubexBase\Application\Http\Components\Button\Button;
use CubexBase\Application\Http\Views\Home\HomeView;
use Packaged\SafeHtml\SafeHtml;

/**
* @var HomeView $this
Expand Down
15 changes: 13 additions & 2 deletions src/MainApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cubex\Events\Handle\ResponsePreSendHeadersEvent;
use Cubex\Sitemap\SitemapListener;
use CubexBase\Application\Context\AppContext;
use CubexBase\Application\Context\Providers\FlashMessageProvider;
use Exception;
use MrEssex\FileCache\AbstractCache;
use MrEssex\FileCache\ApcuCache;
Expand Down Expand Up @@ -36,10 +37,11 @@ class MainApplication extends Application

public function __construct(Cubex $cubex)
{
parent::__construct($cubex);
$cache = new ApcuCache(); //new FileCache();
$cache->setTtl(30);
self::$_cache = $cache;

parent::__construct($cubex);
}

public function handle(Context $c): SResponse
Expand All @@ -62,6 +64,15 @@ protected function _initialize(): void
{
parent::_initialize();
$this->_initDispatch();

$cubex = @$this->getCubex();
$cubex->aliasAbstract(AppContext::class, Context::class);
$cubex->onAfterResolve(function ($inst) {
if($inst instanceof Context)
{
$inst->setContext($this->getContext());
}
});
}

/**
Expand Down Expand Up @@ -217,7 +228,7 @@ protected function _setupCookies(ResponsePreSendHeadersEvent $event)
$ctx = $event->getContext();

// Add flash messages to cookies
$flash = $ctx->flash();
$flash = $ctx->getCubex()->retrieve(FlashMessageProvider::class);
if($flash->hasMessages())
{
$response->headers->setCookie(
Expand Down

0 comments on commit f2ab412

Please sign in to comment.