Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No implicit dependencies #160

Merged
merged 32 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4e1a1df
Added `TestCase::app()` (package).
LastDragon-ru Apr 22, 2024
9291e38
[core] No `illuminate/container`.
LastDragon-ru Apr 22, 2024
46aa81d
[eloquent] No `illuminate/container`.
LastDragon-ru Apr 22, 2024
3f40c86
No `Container` in `SeederService`.
LastDragon-ru Apr 23, 2024
17c5605
[docs] No `illuminate/container` (added `Example::app()`).
LastDragon-ru Apr 22, 2024
bf39814
No `Container` in `RawSqlHelper`.
LastDragon-ru Apr 23, 2024
de841d3
No `Container` in tests.
LastDragon-ru Apr 23, 2024
0fc1a77
[migrator] No `illuminate/container` (except migrations).
LastDragon-ru Apr 23, 2024
f879bda
[dev] No `illuminate/container`.
LastDragon-ru Apr 23, 2024
84892eb
[formatter] No `illuminate/container` (except to get app and config).
LastDragon-ru Apr 23, 2024
943ee20
[documentator] No `illuminate/container` (except to create instance o…
LastDragon-ru Apr 23, 2024
f06a2f3
[graphql] No `illuminate/container` (where possible).
LastDragon-ru Apr 23, 2024
a3043f6
[serializer] No `illuminate/container` (except config and eloquent ca…
LastDragon-ru Apr 23, 2024
48804ab
[spa] No `illuminate/container` (except config and routes).
LastDragon-ru Apr 23, 2024
0d31bfa
[testing] No `illuminate/container` (where possible).
LastDragon-ru Apr 23, 2024
4097b1f
[core] Resolvers for Application, Container and Config (will be used …
LastDragon-ru Apr 24, 2024
d642315
No direct `\Illuminate\Contracts\Foundation\Application` injection.
LastDragon-ru Apr 24, 2024
4db8e61
No direct `\Illuminate\Contracts\Config\Repository` injection.
LastDragon-ru Apr 24, 2024
087aa96
No direct `\Illuminate\Container\Container::getInstance()` call (inco…
LastDragon-ru Apr 25, 2024
b6ffe25
[graphql] `HandlerDirective` will create `HandlerContextOperators` th…
LastDragon-ru Apr 25, 2024
3d94ebf
[graphql] Removed `WithManipulator` (to avoid implicit `Container::ge…
LastDragon-ru Apr 25, 2024
24b7deb
No direct `\Illuminate\Container\Container::getInstance()` call (tests).
LastDragon-ru Apr 26, 2024
4ef45f8
[graphql]: No direct `\Illuminate\Container\Container::getInstance()`…
LastDragon-ru Apr 28, 2024
6e49334
[core] `Translator` helper constructor args cleanup.
LastDragon-ru Apr 26, 2024
e2b3c0f
[core] `Viewer` helper constructor args cleanup.
LastDragon-ru Apr 26, 2024
b53151f
[graphql] `BuilderHelperFactory::getContainer()` => `BuilderHelperFac…
LastDragon-ru Apr 26, 2024
483a860
[core] New way to register routes.
LastDragon-ru Apr 26, 2024
7e6e456
[graphql]: No direct `\Illuminate\Container\Container::getInstance()`…
LastDragon-ru May 2, 2024
8aa9c95
Code cleanup.
LastDragon-ru May 3, 2024
195010a
Dependencies fix.
LastDragon-ru May 3, 2024
daba652
[dev] `Example::app()` will not be used in examples (replaced to `app…
LastDragon-ru May 3, 2024
d1331a9
docs: bc
LastDragon-ru May 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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