Skip to content

Commit 530c226

Browse files
authored
fix: make package dependencies optional where possible (#1624)
1 parent 2e5e4b1 commit 530c226

20 files changed

+890
-834
lines changed

packages/auth/src/Installer/AuthenticationInstaller.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,42 @@
1515
use function Tempest\src_path;
1616
use function Tempest\Support\Namespace\to_fqcn;
1717

18-
final class AuthenticationInstaller implements Installer
19-
{
20-
use PublishesFiles;
21-
22-
private(set) string $name = 'auth';
23-
24-
public function __construct(
25-
private readonly MigrationManager $migrationManager,
26-
private readonly Container $container,
27-
private readonly Console $console,
28-
private readonly ConsoleArgumentBag $consoleArgumentBag,
29-
) {}
30-
31-
public function install(): void
18+
if (class_exists(\Tempest\Console\ConsoleCommand::class, false)) {
19+
final class AuthenticationInstaller implements Installer
3220
{
33-
$migration = $this->publish(__DIR__ . '/basic-user/CreateUsersTableMigration.stub.php', src_path('Authentication/CreateUsersTable.php'));
34-
$this->publish(__DIR__ . '/basic-user/UserModel.stub.php', src_path('Authentication/User.php'));
35-
$this->publishImports();
36-
37-
if ($migration && $this->shouldMigrate()) {
38-
$this->migrationManager->executeUp(
39-
migration: $this->container->get(to_fqcn($migration, root: root_path())),
40-
);
21+
use PublishesFiles;
22+
23+
private(set) string $name = 'auth';
24+
25+
public function __construct(
26+
private readonly MigrationManager $migrationManager,
27+
private readonly Container $container,
28+
private readonly Console $console,
29+
private readonly ConsoleArgumentBag $consoleArgumentBag,
30+
) {}
31+
32+
public function install(): void
33+
{
34+
$migration = $this->publish(__DIR__ . '/basic-user/CreateUsersTableMigration.stub.php', src_path('Authentication/CreateUsersTable.php'));
35+
$this->publish(__DIR__ . '/basic-user/UserModel.stub.php', src_path('Authentication/User.php'));
36+
$this->publishImports();
37+
38+
if ($migration && $this->shouldMigrate()) {
39+
$this->migrationManager->executeUp(
40+
migration: $this->container->get(to_fqcn($migration, root: root_path())),
41+
);
42+
}
4143
}
42-
}
4344

44-
private function shouldMigrate(): bool
45-
{
46-
$argument = $this->consoleArgumentBag->get('migrate');
45+
private function shouldMigrate(): bool
46+
{
47+
$argument = $this->consoleArgumentBag->get('migrate');
4748

48-
if ($argument === null || ! is_bool($argument->value)) {
49-
return $this->console->confirm('Do you want to execute migrations?', default: false);
50-
}
49+
if ($argument === null || ! is_bool($argument->value)) {
50+
return $this->console->confirm('Do you want to execute migrations?', default: false);
51+
}
5152

52-
return (bool) $argument->value;
53+
return (bool) $argument->value;
54+
}
5355
}
5456
}

packages/cache/src/Commands/CacheClearCommand.php

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -22,105 +22,107 @@
2222
use function Tempest\Support\arr;
2323
use function Tempest\Support\str;
2424

25-
final readonly class CacheClearCommand
26-
{
27-
use HasConsole;
28-
29-
private const string DEFAULT_CACHE = 'default';
30-
31-
public function __construct(
32-
private Cache $cache,
33-
private Container $container,
34-
) {}
35-
36-
#[ConsoleCommand(name: 'cache:clear', description: 'Clears all or specified caches', middleware: [ForceMiddleware::class, CautionMiddleware::class])]
37-
public function __invoke(
38-
#[ConsoleArgument(description: 'Name of the tagged cache to clear')]
39-
?string $tag = null,
40-
#[ConsoleCommand(description: 'Whether to clear all caches')]
41-
bool $all = false,
42-
#[ConsoleCommand(description: 'Whether to clear internal caches')]
43-
bool $internal = false,
44-
): void {
45-
if (! $this->container instanceof GenericContainer) {
46-
$this->console->error('Clearing caches is only available when using the default container.');
47-
return;
48-
}
49-
50-
if ($internal) {
51-
$this->clearInternalCaches($all);
52-
} else {
53-
$this->clearUserCaches($tag, $all);
54-
}
55-
}
56-
57-
private function clearInternalCaches(bool $all = false): void
25+
if (class_exists(\Tempest\Console\ConsoleCommand::class, false)) {
26+
final readonly class CacheClearCommand
5827
{
59-
$caches = [ConfigCache::class, ViewCache::class, IconCache::class, DiscoveryCache::class];
60-
61-
if ($all === false && count($caches) > 1) {
62-
$caches = $this->ask(
63-
question: 'Which caches do you want to clear?',
64-
options: $caches,
65-
multiple: true,
66-
);
28+
use HasConsole;
29+
30+
private const string DEFAULT_CACHE = 'default';
31+
32+
public function __construct(
33+
private Cache $cache,
34+
private Container $container,
35+
) {}
36+
37+
#[ConsoleCommand(name: 'cache:clear', description: 'Clears all or specified caches', middleware: [ForceMiddleware::class, CautionMiddleware::class])]
38+
public function __invoke(
39+
#[ConsoleArgument(description: 'Name of the tagged cache to clear')]
40+
?string $tag = null,
41+
#[ConsoleCommand(description: 'Whether to clear all caches')]
42+
bool $all = false,
43+
#[ConsoleCommand(description: 'Whether to clear internal caches')]
44+
bool $internal = false,
45+
): void {
46+
if (! $this->container instanceof GenericContainer) {
47+
$this->console->error('Clearing caches is only available when using the default container.');
48+
return;
49+
}
50+
51+
if ($internal) {
52+
$this->clearInternalCaches($all);
53+
} else {
54+
$this->clearUserCaches($tag, $all);
55+
}
6756
}
6857

69-
if (count($caches) === 0) {
70-
$this->console->info('No cache selected.');
71-
return;
58+
private function clearInternalCaches(bool $all = false): void
59+
{
60+
$caches = [ConfigCache::class, ViewCache::class, IconCache::class, DiscoveryCache::class];
61+
62+
if ($all === false && count($caches) > 1) {
63+
$caches = $this->ask(
64+
question: 'Which caches do you want to clear?',
65+
options: $caches,
66+
multiple: true,
67+
);
68+
}
69+
70+
if (count($caches) === 0) {
71+
$this->console->info('No cache selected.');
72+
return;
73+
}
74+
75+
$this->console->header('Internal caches');
76+
77+
foreach ($caches as $cache) {
78+
$cache = $this->container->get($cache);
79+
$cache->clear();
80+
81+
$this->console->keyValue(
82+
key: $cache::class,
83+
value: "<style='bold fg-green'>CLEARED</style>",
84+
);
85+
}
7286
}
7387

74-
$this->console->header('Internal caches');
75-
76-
foreach ($caches as $cache) {
77-
$cache = $this->container->get($cache);
78-
$cache->clear();
79-
80-
$this->console->keyValue(
81-
key: $cache::class,
82-
value: "<style='bold fg-green'>CLEARED</style>",
83-
);
84-
}
85-
}
86-
87-
private function clearUserCaches(?string $tag = null, bool $all = false): void
88-
{
89-
if ($tag && $all) {
90-
$this->console->error('You cannot specify both a tag and clear all caches.');
91-
return;
92-
}
93-
94-
/** @var GenericContainer $container */
95-
$container = $this->container;
96-
$cacheTags = arr($container->getSingletons(CacheConfig::class))
97-
->map(fn ($_, string $key) => $key === CacheConfig::class ? self::DEFAULT_CACHE : Str\after_last($key, '#'))
98-
->filter(fn ($_, string $key) => in_array($tag, [null, self::DEFAULT_CACHE], strict: true) ? true : str($key)->afterLast('#')->equals($tag))
99-
->values();
100-
101-
if ($all === false && count($cacheTags) > 1) {
102-
$cacheTags = $this->ask(
103-
question: 'Which caches do you want to clear?',
104-
options: $cacheTags,
105-
multiple: true,
106-
);
107-
}
108-
109-
if (count($cacheTags) === 0) {
110-
$this->console->info('No cache selected.');
111-
return;
112-
}
113-
114-
$this->console->header('User caches');
115-
116-
foreach ($cacheTags as $tag) {
117-
$cache = $this->container->get(Cache::class, $tag === self::DEFAULT_CACHE ? null : $tag);
118-
$cache->clear();
119-
120-
$this->console->keyValue(
121-
key: $tag,
122-
value: "<style='bold fg-green'>CLEARED</style>",
123-
);
88+
private function clearUserCaches(?string $tag = null, bool $all = false): void
89+
{
90+
if ($tag && $all) {
91+
$this->console->error('You cannot specify both a tag and clear all caches.');
92+
return;
93+
}
94+
95+
/** @var GenericContainer $container */
96+
$container = $this->container;
97+
$cacheTags = arr($container->getSingletons(CacheConfig::class))
98+
->map(fn ($_, string $key) => $key === CacheConfig::class ? self::DEFAULT_CACHE : Str\after_last($key, '#'))
99+
->filter(fn ($_, string $key) => in_array($tag, [null, self::DEFAULT_CACHE], strict: true) ? true : str($key)->afterLast('#')->equals($tag))
100+
->values();
101+
102+
if ($all === false && count($cacheTags) > 1) {
103+
$cacheTags = $this->ask(
104+
question: 'Which caches do you want to clear?',
105+
options: $cacheTags,
106+
multiple: true,
107+
);
108+
}
109+
110+
if (count($cacheTags) === 0) {
111+
$this->console->info('No cache selected.');
112+
return;
113+
}
114+
115+
$this->console->header('User caches');
116+
117+
foreach ($cacheTags as $tag) {
118+
$cache = $this->container->get(Cache::class, $tag === self::DEFAULT_CACHE ? null : $tag);
119+
$cache->clear();
120+
121+
$this->console->keyValue(
122+
key: $tag,
123+
value: "<style='bold fg-green'>CLEARED</style>",
124+
);
125+
}
124126
}
125127
}
126128
}

0 commit comments

Comments
 (0)