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

Commit

Permalink
Update how we use providers
Browse files Browse the repository at this point in the history
General improvements
  • Loading branch information
MrEssex committed Oct 2, 2023
1 parent 384f6b2 commit 29817fa
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 84 deletions.
3 changes: 3 additions & 0 deletions conf/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ host = cubexbase.local-host.xyz
[dispatch]
opt-sourcemap = false
opt-webp = false

[site]
title = My Cubex Project
3 changes: 3 additions & 0 deletions src/Cache/CacheObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Packaged\Dal\Cache\CacheDao;
use Packaged\Dal\Exceptions\DataStore\DaoNotFoundException;

/**
* Use the Cache DAO (see conf) to store and retrieve data
*/
abstract class CacheObject extends CacheDao
{
final protected function __construct(protected string $_cacheKey = "")
Expand Down
25 changes: 15 additions & 10 deletions src/Context/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cubex\I18n\GetTranslatorTrait;
use CubexBase\Application\Context\Providers\DatabaseProvider;
use CubexBase\Application\Context\Providers\FlashMessageProvider;
use CubexBase\Application\Context\Providers\SeoProvider;
use Packaged\Http\LinkBuilder\LinkBuilder;
use Packaged\I18n\Translatable;
use Packaged\I18n\TranslatableTrait;
Expand All @@ -15,14 +16,18 @@ class AppContext extends Context implements Translatable
use TranslatableTrait;
use GetTranslatorTrait;

protected ?FlashMessageProvider $flash = null;

protected bool $_dbConfigured = false;

protected function _initialize(): void
{
parent::_initialize();
DatabaseProvider::instance($this)->registerDatabaseConnections($this->getEnvironment());
$cubex = $this->getCubex();

//Register the database
$databaseProvider = DatabaseProvider::instance($this);
$databaseProvider->registerDatabaseConnections($this->getEnvironment());

$cubex->share(SeoProvider::class, SeoProvider::instance($this));
$cubex->share(DatabaseProvider::class, $databaseProvider);
$cubex->share(FlashMessageProvider::class, FlashMessageProvider::instance($this, $this->request()));
}

/**
Expand All @@ -38,11 +43,11 @@ public function linkBuilder(string $path = '', array $query = []): LinkBuilder

public function flash(): FlashMessageProvider
{
if($this->flash === null)
{
$this->flash = FlashMessageProvider::hydrateFromRequest($this->request());
}
return $this->getCubex()->retrieve(FlashMessageProvider::class);
}

return $this->flash;
public function seo(): SeoProvider
{
return $this->getCubex()->retrieve(SeoProvider::class);
}
}
9 changes: 2 additions & 7 deletions src/Context/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,18 @@ abstract class AbstractProvider implements ContextAware, WithContext

protected static ?self $_instance = null;

/**
* AbstractProvider constructor.
*/
private function __construct() { }

/**
* Returns an instance of the provider
*
* @param Context $context
*
* @return static
*/
public static function instance(Context $context): static
public static function instance(Context $context, ...$args): static
{
if(!static::$_instance instanceof static)
{
static::$_instance = static::withContext($context);
static::$_instance = static::withContext($context, ...$args);
}

return static::$_instance;
Expand Down
26 changes: 14 additions & 12 deletions src/Context/Providers/FlashMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

namespace CubexBase\Application\Context\Providers;

use Packaged\Context\Context;
use Packaged\Http\Cookies\Cookie;
use Packaged\Http\Request;

class FlashMessageProvider
/**
* @method static FlashMessageProvider instance(Context $context, ...$args)
*/
class FlashMessageProvider extends AbstractProvider
{
protected array $_messages = [];

public function __construct(Request $request)
{
$cookie = $request->cookies->get('flash');
if($cookie)
{
$this->_messages = json_decode($cookie, true);
}
}

public function hasMessages(): bool
{
return !empty($this->_messages);
Expand All @@ -34,15 +47,4 @@ public function toCookie(): Cookie
{
return new Cookie('flash', json_encode($this->_messages), time() + 60);
}

public static function hydrateFromRequest(Request $request): FlashMessageProvider
{
$flash = new self();
$cookie = $request->cookies->get('flash');
if($cookie)
{
$flash->_messages = json_decode($cookie, true);
}
return $flash;
}
}
97 changes: 97 additions & 0 deletions src/Context/Providers/SeoProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace CubexBase\Application\Context\Providers;

use Packaged\Context\Context;
use Packaged\Glimpse\Core\CustomHtmlTag;

/**
* @method static SeoProvider instance(Context $context)
*/
class SeoProvider extends AbstractProvider
{
protected array $tags = [];
protected array $twitterTags = [];
protected array $openGraphTags = [];

protected string $title = '';

public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}

public function meta(string $name, string $value)
{
$this->_push('meta', ['name' => $name, 'content' => $value]);
return $this;
}

public function og(string $name, string $value)
{
$this->openGraphTags[] = [
'meta',
[
'property' => 'og:' . $name,
'content' => $value,
],
];
return $this;
}

public function twitter(string $name, string $value)
{
$this->twitterTags[] = [
'meta',
[
'property' => 'twitter:' . $name,
'content' => $value,
],
];
return $this;
}

protected function _push(string $name, array $attrs)
{
foreach($attrs as $k => $v)
{
$attrs[$k] = $v;
}

$this->tags[] = [$name, $attrs];
return $this;
}

protected function _build(array $tags)
{
$out = '';
foreach($tags as $tag)
{
$attrs = [];
foreach($tag[1] as $k => $v)
{
$attrs[$k] = $v;
}
$out .= CustomHtmlTag::build($tag['0'], $attrs, '');
}

return $out;
}

public function __toString(): string
{
// Default Site Title
if(empty($this->title))
{
$config = $this->getContext()->config()->getSection('site');
$this->title = $config->getItem('title', 'Default Site Title');
$this->og('title', $this->title);
}

$title = CustomHtmlTag::build('title', [], $this->title);
return $title . $this->_build($this->tags) .
$this->_build($this->twitterTags) .
$this->_build($this->openGraphTags);
}
}
3 changes: 3 additions & 0 deletions src/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function getHome(AppContext $ctx)
$form->hydrate($ctx->request()->request->all());
$form->hydrate($ctx->request()->query->all());

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

return HomeView::withContext($this)
->setForm($form);
}
Expand Down
18 changes: 5 additions & 13 deletions src/Http/Layout/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Packaged\Context\WithContextTrait;
use Packaged\Dispatch\ResourceManager;
use Packaged\Ui\Element;
use RuntimeException;

/**
* @method AppContext getContext();
*/
class Layout extends Element implements ContextAware, WithContext
{
use ContextAwareTrait;
Expand All @@ -22,21 +24,11 @@ class Layout extends Element implements ContextAware, WithContext
protected mixed $_header;
protected mixed $_footer;

public function getContext(): AppContext
{
if($this->_context instanceof AppContext)
{
return $this->_context;
}

throw new RuntimeException('Invalid Context Passed through');
}

public function render(): string
public function __construct()
{
// Require default resources
ResourceManager::resources()->requireCss('main.min.css');
ResourceManager::resources()->requireJs('main.min.js');
return parent::render();
}

public function getContent()
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Layout/Layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ $store = Dispatch::instance()->store();
<link rel="apple-touch-icon" href="<?= $ctx->linkBuilder('/icon.png') ?>">
<link type="text/plain" rel="author" href="<?= $ctx->linkBuilder('/humans.txt') ?>">
<link rel="manifest" href="<?= $ctx->linkBuilder('/site.webmanifest') ?>">
<title>Cubex Skeleton</title>

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

<?= $store->generateHtmlIncludes() ?>
</head>
Expand Down
33 changes: 1 addition & 32 deletions src/Http/Layout/LayoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,16 @@

namespace CubexBase\Application\Http\Layout;

use Cubex\Controller\AuthedController;
use Cubex\I18n\GetTranslatorTrait;
use CubexBase\Application\Context\AppContext;
use CubexBase\Application\Http\Middleware\WithMiddlewareTrait;
use CubexBase\Application\Http\Views\AbstractView;
use CubexBase\Application\MainApplication;
use Exception;
use MrEssex\FileCache\Exceptions\InvalidArgumentException;
use Packaged\Context\Context;
use Packaged\Context\WithContext;
use Packaged\Context\WithContextTrait;
use Packaged\I18n\Translatable;
use Packaged\I18n\TranslatableTrait;
use Packaged\I18n\Translators\Translator;
use Packaged\Ui\Element;
use Packaged\Ui\Html\HtmlElement;
use RuntimeException;
use function is_array;
use function is_scalar;

abstract class LayoutController extends AuthedController implements WithContext, Translatable, Translator
abstract class LayoutController extends WithErrorController
{
use GetTranslatorTrait;
use TranslatableTrait;
use WithContextTrait;
use WithMiddlewareTrait;
use ErrorPageTrait;

/**
* @throws Exception
*/
public function getContext(): Context
{
if(parent::getContext() instanceof AppContext)
{
return parent::getContext();
}

throw new RuntimeException('Invalid Context Passed through');
}

/**
* @throws InvalidArgumentException
* @throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

namespace CubexBase\Application\Http\Layout;

use Cubex\Controller\AuthedController;
use Cubex\I18n\GetTranslatorTrait;
use CubexBase\Application\Context\AppContext;
use CubexBase\Application\Http\Middleware\WithMiddlewareTrait;
use CubexBase\Application\Http\Views\Error\ErrorView;
use Packaged\Context\Context;
use Packaged\Context\WithContext;
use Packaged\Context\WithContextTrait;
use Packaged\Http\Response;
use Packaged\I18n\Translatable;
use Packaged\I18n\TranslatableTrait;
use Packaged\I18n\Translators\Translator;
use Packaged\Routing\Handler\Handler;

trait ErrorPageTrait
abstract class WithErrorController extends AuthedController implements WithContext, Translatable, Translator
{
use GetTranslatorTrait;
use TranslatableTrait;
use WithContextTrait;
use WithMiddlewareTrait;

protected function _getHandler(Context $context): callable|string|Handler
{
$handler = parent::_getHandler($context);
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Views/AbstractView.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ abstract class AbstractView extends AbstractBase
{
use TemplateLoaderTrait;

public function getHeader(): ?AbstractBase { return null; }

public function getFooter(): ?AbstractBase { return null; }

public function shouldCache(): bool
{
return !$this->getContext()->matches(ExpectEnvironment::local());
}

public function getHeader(): ?AbstractBase { return null; }

public function getFooter(): ?AbstractBase { return null; }

public function shouldIndex(): bool { return true; }
}
Loading

0 comments on commit 29817fa

Please sign in to comment.