Remove unneeded dependency from App

This commit is contained in:
Giuseppe Criscione 2024-11-24 15:34:27 +01:00
parent 59586e5976
commit f2f588d2dc
6 changed files with 46 additions and 60 deletions

View File

@ -207,6 +207,7 @@ final class App
$container->define(Statistics::class)
->parameter('path', fn (Config $config) => $config->get('system.statistics.path'))
->parameter('translation', fn (Translations $translations) => $translations->getCurrent())
->alias('statistics');
$container->define(FilesCache::class)

View File

@ -2,11 +2,11 @@
namespace Formwork\Statistics;
use Formwork\App;
use Formwork\Http\Request;
use Formwork\Http\Utils\IpAnonymizer;
use Formwork\Http\Utils\Visitor;
use Formwork\Log\Registry;
use Formwork\Translations\Translation;
use Formwork\Utils\Arr;
use Formwork\Utils\Date;
use Formwork\Utils\FileSystem;
@ -67,7 +67,7 @@ class Statistics
/**
* Create a new Statistics instance
*/
public function __construct(string $path, protected App $app, protected Request $request)
public function __construct(string $path, protected Request $request, protected Translation $translation)
{
if (!FileSystem::exists($path)) {
FileSystem::createDirectory($path);
@ -123,7 +123,7 @@ class Statistics
$labels = Arr::map(
iterator_to_array($this->generateDays($limit)),
fn (string $day): string => Date::formatTimestamp(Date::toTimestamp($day, self::DATE_FORMAT), "D\nj M", $this->app->translations()->getCurrent())
fn (string $day): string => Date::formatTimestamp(Date::toTimestamp($day, self::DATE_FORMAT), "D\nj M", $this->translation)
);
return [

View File

@ -3,8 +3,6 @@
namespace Formwork\Templates;
use Closure;
use Formwork\App;
use Formwork\Assets;
use Formwork\Schemes\Scheme;
use Formwork\Site;
use Formwork\Utils\Constraint;
@ -16,19 +14,21 @@ use Stringable;
class Template implements Stringable
{
/**
* Template assets instance
*/
protected Assets $assets;
protected string $path;
/**
* Create a new Template instance
*
* @param array<string, mixed> $vars
* @param array<string, Closure> $methods
*/
public function __construct(protected string $name, protected Scheme $scheme, protected App $app, protected Site $site, protected ViewFactory $viewFactory)
{
$this->path = $this->app->config()->get('system.templates.path');
public function __construct(
protected string $name,
protected array $vars,
protected string $path,
protected array $methods,
protected Scheme $scheme,
protected Site $site,
protected ViewFactory $viewFactory
) {
}
public function __toString(): string
@ -56,17 +56,6 @@ class Template implements Stringable
return $this->path;
}
/**
* Get Assets instance
*/
public function assets(): Assets
{
return $this->assets ?? ($this->assets = new Assets(
FileSystem::joinPaths($this->path, 'assets'),
$this->site->uri('/site/templates/assets/', includeLanguage: false)
));
}
/**
* Render template
*
@ -94,38 +83,14 @@ class Template implements Stringable
$view = $this->viewFactory->make(
$this->name,
[...$this->defaultVars(), ...$vars, ...$controllerVars],
[...$this->vars, ...$vars, ...$controllerVars],
$this->path,
[...$this->defaultMethods()]
[...$this->methods]
);
return $view->render();
}
/**
* @return array<string, mixed>
*/
protected function defaultVars(): array
{
return [
'router' => $this->app->router(),
'site' => $this->site,
'csrfToken' => $this->app->getService('csrfToken'),
];
}
/**
* Default template methods
*
* @return array<string, Closure>
*/
protected function defaultMethods(): array
{
return [
'assets' => fn () => $this->assets(),
];
}
/**
* Load template controller if exists
*
@ -138,7 +103,7 @@ class Template implements Stringable
$controllerFile = FileSystem::joinPaths($this->path, 'controllers', $this->name . '.php');
if (FileSystem::exists($controllerFile)) {
return (array) Renderer::load($controllerFile, [...$this->defaultVars(), ...$vars], $this);
return (array) Renderer::load($controllerFile, [...$this->vars, ...$vars], $this);
}
return null;

View File

@ -2,19 +2,40 @@
namespace Formwork\Templates;
use Formwork\App;
use Formwork\Assets;
use Formwork\Config\Config;
use Formwork\Schemes\Schemes;
use Formwork\Security\CsrfToken;
use Formwork\Services\Container;
use Formwork\Utils\FileSystem;
class TemplateFactory
{
public function __construct(protected Container $container, protected Schemes $schemes)
public function __construct(protected Container $container, protected App $app, protected Config $config, protected Schemes $schemes)
{
}
public function make(string $name): Template
{
$path = $this->config->get('system.templates.path');
$assets = new Assets(
FileSystem::joinPaths($path, 'assets'),
$this->app->site()->uri('/site/templates/assets/', includeLanguage: false)
);
return $this->container->build(Template::class, [
'name' => $name,
'name' => $name,
'path' => $path,
'methods' => [
'assets' => fn () => $assets,
],
'vars' => [
'router' => $this->app->router(),
'site' => $this->app->site(),
'csrfToken' => $this->app->getService(CsrfToken::class),
],
'scheme' => $this->schemes->get('pages.' . $name),
]);
}

View File

@ -2,7 +2,6 @@
namespace Formwork\Users;
use Formwork\App;
use Formwork\Config\Config;
use Formwork\Files\FileFactory;
use Formwork\Http\Request;
@ -10,6 +9,7 @@ use Formwork\Images\Image;
use Formwork\Log\Registry;
use Formwork\Model\Model;
use Formwork\Panel\Security\Password;
use Formwork\Schemes\Schemes;
use Formwork\Users\Exceptions\AuthenticationFailedException;
use Formwork\Users\Exceptions\UserImageNotFoundException;
use Formwork\Users\Exceptions\UserNotLoggedException;
@ -53,9 +53,9 @@ class User extends Model
*
* @param array<string, mixed> $data
*/
public function __construct(array $data, protected Role $role, protected App $app, protected Config $config, protected Request $request, protected FileFactory $fileFactory)
public function __construct(array $data, protected Role $role, protected Schemes $schemes, protected Config $config, protected Request $request, protected FileFactory $fileFactory)
{
$this->scheme = $app->schemes()->get('users.user');
$this->scheme = $this->schemes->get('users.user');
$this->fields = $this->scheme->fields();
$this->fields->setModel($this);

View File

@ -1,6 +1,5 @@
<?php
use Formwork\App;
use Formwork\Config\Config;
use Formwork\Http\JsonResponse;
use Formwork\Http\RedirectResponse;
@ -315,7 +314,7 @@ return [
],
'panel.register' => [
'action' => static function (Request $request, App $app, Site $site, Panel $panel) {
'action' => static function (Request $request, Site $site, Panel $panel) {
// Register panel if no user exists
if ($site->users()->isEmpty()) {
if (!$request->isLocalhost()) {