Skip to content

Commit

Permalink
refactor!: No implicit dependencies (#160)
Browse files Browse the repository at this point in the history
Issue: #151
  • Loading branch information
LastDragon-ru committed May 3, 2024
2 parents 73d3a0e + d1331a9 commit ac03deb
Show file tree
Hide file tree
Showing 184 changed files with 1,387 additions and 1,058 deletions.
1 change: 1 addition & 0 deletions docs/Shared/Upgrade/FromV6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [ ] Direct usages of `Container::getInstances()` were replaced by explicit constructor parameters. You may need to update your code accordingly (#151).
12 changes: 12 additions & 0 deletions packages/core/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)

[//]: # (end: c70a9a43c0a80bd2e7fa6010a9b2c0fbcab4cb4d536d7a498216d9df7431f7e2)

# Upgrade from v6

[include:file]: ../../docs/Shared/Upgrade/FromV6.md
[//]: # (start: 6b55bf5daea407a4590344596d41efd8368a783e2666bfe431a88a5eeaff3a95)
[//]: # (warning: Generated automatically. Do not edit.)

* [ ] Direct usages of `Container::getInstances()` were replaced by explicit constructor parameters. You may need to update your code accordingly (#151).

[//]: # (end: 6b55bf5daea407a4590344596d41efd8368a783e2666bfe431a88a5eeaff3a95)

* [ ] `\LastDragon_ru\LaraASP\Core\Provider\WithRoutes::bootRoutes()` requires settings.

# Upgrade from v5

[include:file]: ../../docs/Shared/Upgrade/FromV5.md
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/Helpers/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
abstract class Translator {
public function __construct(
protected TranslatorContract $translator,
protected string $package,
protected string|null $group = 'messages',
) {
// empty
}

/**
* Should return the name of the package.
*/
abstract protected function getName(): string;

/**
* @param list<string>|string $key
* @param array<string, mixed> $replace
Expand Down Expand Up @@ -59,9 +62,7 @@ public function setLocale(string $locale): static {
}

protected function key(string $key): string {
return $this->group
? "{$this->package}::{$this->group}.{$key}"
: "{$this->package}::{$key}";
return "{$this->getName()}::messages.{$key}";
}

/**
Expand Down
32 changes: 19 additions & 13 deletions packages/core/src/Helpers/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace LastDragon_ru\LaraASP\Core\Helpers;

use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use LastDragon_ru\LaraASP\Core\Helpers\Translator as PackageTranslator;
use LastDragon_ru\LaraASP\Core\Package;
use LastDragon_ru\LaraASP\Core\Testing\Package\TestCase;
use LastDragon_ru\LaraASP\Testing\Utils\WithTranslations;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

Expand Down Expand Up @@ -36,9 +36,12 @@ public function testChoice(
): void {
$this->setTranslations($translations);

$implementation = Container::getInstance()->make(Translator::class);
$translator = new class($implementation, Package::Name, null) extends PackageTranslator {
// empty
$implementation = $this->app()->make(Translator::class);
$translator = new class($implementation) extends PackageTranslator {
#[Override]
protected function getName(): string {
return Package::Name;
}
};

self::assertEquals($expected, $translator->choice($key, $number, $replace, $locale));
Expand All @@ -59,9 +62,12 @@ public function testGet(
): void {
$this->setTranslations($translations);

$implementation = Container::getInstance()->make(Translator::class);
$translator = new class($implementation, Package::Name, null) extends PackageTranslator {
// empty
$implementation = $this->app()->make(Translator::class);
$translator = new class($implementation) extends PackageTranslator {
#[Override]
protected function getName(): string {
return Package::Name;
}
};

self::assertEquals($expected, $translator->get($key, $replace, $locale));
Expand Down Expand Up @@ -89,7 +95,7 @@ public static function dataProviderGet(): array {
static function (TestCase $test, string $currentLocale, string $fallbackLocale): array {
return [
$currentLocale => [
Package::Name.'::should.be.translated' => 'translated :value',
Package::Name.'::messages.should.be.translated' => 'translated :value',
],
];
},
Expand All @@ -104,7 +110,7 @@ static function (TestCase $test, string $currentLocale, string $fallbackLocale):
static function (TestCase $test, string $currentLocale, string $fallbackLocale): array {
return [
$currentLocale => [
Package::Name.'::should.be.translated' => 'translated',
Package::Name.'::messages.should.be.translated' => 'translated',
],
];
},
Expand All @@ -119,7 +125,7 @@ static function (TestCase $test, string $currentLocale, string $fallbackLocale):
static function (TestCase $test, string $currentLocale, string $fallbackLocale): array {
return [
'unk' => [
Package::Name.'::should.be.translated' => 'translated :value',
Package::Name.'::messages.should.be.translated' => 'translated :value',
],
];
},
Expand Down Expand Up @@ -152,7 +158,7 @@ public static function dataProviderChoice(): array {
static function (TestCase $test, string $currentLocale, string $fallbackLocale): array {
return [
$currentLocale => [
Package::Name.'::should.be.translated' => '{1} one |[2,*] translated :value',
Package::Name.'::messages.should.be.translated' => '{1} one |[2,*] translated :value',
],
];
},
Expand All @@ -168,7 +174,7 @@ static function (TestCase $test, string $currentLocale, string $fallbackLocale):
static function (TestCase $test, string $currentLocale, string $fallbackLocale): array {
return [
$currentLocale => [
Package::Name.'::should.be.translated' => '{1} one |[2,*] translated :value',
Package::Name.'::messages.should.be.translated' => '{1} one |[2,*] translated :value',
],
];
},
Expand All @@ -184,7 +190,7 @@ static function (TestCase $test, string $currentLocale, string $fallbackLocale):
static function (TestCase $test, string $currentLocale, string $fallbackLocale): array {
return [
'unk' => [
Package::Name.'::should.be.translated' => '{1} one |[2,*] translated :value',
Package::Name.'::messages.should.be.translated' => '{1} one |[2,*] translated :value',
],
];
},
Expand Down
21 changes: 6 additions & 15 deletions packages/core/src/Helpers/Viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
*/
abstract class Viewer {
public function __construct(
protected readonly Translator $translator,
protected readonly ViewFactoryContract $factory,
protected readonly string $package,
) {
// empty
}

/**
* Should return the name of the package.
*/
abstract protected function getName(): string;

/**
* @param array<string, mixed> $data
*/
public function get(string $view, array $data = []): ViewContract {
return $this->factory->make(
"{$this->package}::{$view}",
$this->getDefaultData() + $data,
);
return $this->factory->make("{$this->getName()}::{$view}", $data);
}

/**
Expand All @@ -33,13 +33,4 @@ public function get(string $view, array $data = []): ViewContract {
public function render(string $view, array $data = []): string {
return $this->get($view, $data)->render();
}

/**
* @return array<string, mixed>
*/
protected function getDefaultData(): array {
return [
'translator' => $this->translator,
];
}
}
41 changes: 25 additions & 16 deletions packages/core/src/Helpers/ViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\View\View as ViewContract;
use LastDragon_ru\LaraASP\Core\Testing\Package\TestCase;
use Mockery;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;

/**
Expand All @@ -14,40 +15,48 @@
#[CoversClass(Viewer::class)]
final class ViewerTest extends TestCase {
public function testGet(): void {
$view = 'view';
$data = ['a' => 123];
$package = 'package';
$translator = Mockery::mock(Translator::class);
$factory = Mockery::mock(ViewFactory::class);
$view = 'view';
$data = ['a' => 123];
$package = 'package';
$factory = Mockery::mock(ViewFactory::class);
$factory
->shouldReceive('make')
->with("{$package}::{$view}", ['translator' => $translator] + $data)
->with("{$package}::{$view}", $data)
->once()
->andReturn(
Mockery::mock(ViewContract::class),
);

$viewer = new class($translator, $factory, $package) extends Viewer {
// empty
$viewer = new class($factory, $package) extends Viewer {
public function __construct(
ViewFactory $factory,
private readonly string $package,
) {
parent::__construct($factory);
}

#[Override]
protected function getName(): string {
return $this->package;
}
};

$viewer->get($view, $data);
}

public function testRender(): void {
$view = 'view';
$data = ['a' => 123];
$package = 'package';
$content = 'content';
$translator = Mockery::mock(Translator::class);
$factory = Mockery::mock(ViewFactory::class);
$template = Mockery::mock(ViewContract::class);
$view = 'view';
$data = ['a' => 123];
$package = 'package';
$content = 'content';
$factory = Mockery::mock(ViewFactory::class);
$template = Mockery::mock(ViewContract::class);
$template
->shouldReceive('render')
->once()
->andReturn($content);

$viewer = Mockery::mock(Viewer::class, [$translator, $factory, $package]);
$viewer = Mockery::mock(Viewer::class, [$factory, $package]);
$viewer->makePartial();
$viewer
->shouldReceive('get')
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/Provider/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

namespace LastDragon_ru\LaraASP\Core\Provider;

use Illuminate\Support\ServiceProvider;
use ReflectionClass;

use function dirname;
use function ltrim;

/**
* @mixin ServiceProvider
*/
trait Helper {
/**
* Should return the name of the package.
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/Provider/WithConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace LastDragon_ru\LaraASP\Core\Provider;

use Illuminate\Container\Container;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Foundation\CachesConfiguration;
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Utils\ConfigMerger;

/**
* @mixin ServiceProvider
* @phpstan-require-extends ServiceProvider
*/
trait WithConfig {
use Helper;
Expand All @@ -26,7 +25,7 @@ protected function bootConfig(): void {

protected function loadConfigFrom(string $path, string $key): void {
if (!($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) {
$repository = Container::getInstance()->make(Repository::class);
$repository = $this->app->make(Repository::class);
$merger = new ConfigMerger();
$target = (array) require $path;
$current = (array) $repository->get($key, []);
Expand Down
32 changes: 29 additions & 3 deletions packages/core/src/Provider/WithRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@

namespace LastDragon_ru\LaraASP\Core\Provider;

use Closure;
use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Support\ServiceProvider;

/**
* @mixin ServiceProvider
* @phpstan-require-extends ServiceProvider
*/
trait WithRoutes {
use Helper;

protected function bootRoutes(): void {
$this->loadRoutesFrom($this->getPath('../defaults/routes.php'));
/**
* @param Closure(): array{enabled: bool, attributes: array<string, mixed>} $settings
*/
protected function bootRoutes(Closure $settings): void {
// Cached?
if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) {
return;
}

// Load (config may be incomplete until boot)
$this->booted(function () use ($settings): void {
// Enabled?
$settings = $settings();

if (!$settings['enabled'] || !$this->app->bound(Registrar::class)) {
return;
}

// Add
$path = $this->getPath('../defaults/routes.php');
$routes = require $path;
$registrar = $this->app->make(Registrar::class);

$registrar->group($settings['attributes'], $routes);
});
}
}
8 changes: 4 additions & 4 deletions packages/core/src/Provider/WithSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace LastDragon_ru\LaraASP\Core\Provider;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Utils\Scheduler;

/**
* @mixin ServiceProvider
* @phpstan-require-extends ServiceProvider
*/
trait WithSchedule {
/**
Expand All @@ -21,8 +21,8 @@ protected function bootSchedule(string ...$jobs): void {

$this->callAfterResolving(
Schedule::class,
static function (Schedule $schedule) use ($jobs): void {
$scheduler = Container::getInstance()->make(Scheduler::class);
static function (Schedule $schedule, Container $container) use ($jobs): void {
$scheduler = $container->make(Scheduler::class);

foreach ($jobs as $job) {
$scheduler->register($schedule, $job);
Expand Down
Loading

0 comments on commit ac03deb

Please sign in to comment.