Skip to content

Commit

Permalink
Fix cache + update changed method names after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Mar 25, 2024
1 parent 242ef15 commit edf44d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'factories' => 'Database\Factories',
],

'cache_folder' => 'bootstrap/cache/ddd/',

'autoload' => [
/*
Expand Down
39 changes: 29 additions & 10 deletions src/Support/DomainAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ protected function registerDomainServiceProviders(bool|string $domainPath = null
{
$domainPath = is_string($domainPath) ? $domainPath : '*/*ServiceProvider.php';

$serviceProviders = Cache::rememberForever('ddd-domain-service-providers', static function () use ($domainPath){
$serviceProviders = $this->remember('ddd-domain-service-providers', static function () use ($domainPath){
return Arr::map(
glob(base_path(DomainResolver::getConfiguredDomainPath().'/'.$domainPath)),
glob(base_path(DomainResolver::domainPath().'/'.$domainPath)),
(static function ($serviceProvider) {

return Path::filePathToNamespace(
$serviceProvider,
DomainResolver::getConfiguredDomainPath(),
DomainResolver::getConfiguredDomainNamespace()
DomainResolver::domainPath(),
DomainResolver::domainRootNamespace()
);
}));
});
Expand All @@ -63,14 +64,14 @@ protected function registerDomainServiceProviders(bool|string $domainPath = null
protected function registerDomainCommands(bool|string $domainPath = null): void
{
$domainPath = is_string($domainPath) ? $domainPath : '*/Commands/*.php';
$commands = Cache::rememberForever('ddd-domain-commands', static function () use ($domainPath){
$commands = $this->remember('ddd-domain-commands', static function () use ($domainPath){
$commands = Arr::map(
glob(base_path(DomainResolver::getConfiguredDomainPath().'/'.$domainPath)),
glob(base_path(DomainResolver::domainPath().'/'.$domainPath)),
static function ($command) {
return Path::filePathToNamespace(
$command,
DomainResolver::getConfiguredDomainPath(),
DomainResolver::getConfiguredDomainNamespace()
DomainResolver::domainPath(),
DomainResolver::domainRootNamespace()
);
});

Expand Down Expand Up @@ -103,7 +104,7 @@ protected function registerPolicies(bool|string $domainPath = null): void
return null;
}

$policy = DomainResolver::getConfiguredDomainNamespace().'\\'.$domain.'\\'.str_replace('{model}', $model, $domainPath);
$policy = DomainResolver::domainRootNamespace().'\\'.$domain.'\\'.str_replace('{model}', $model, $domainPath);

return $policy;
});
Expand Down Expand Up @@ -142,7 +143,7 @@ protected function extractDomainAndModelFromModelNamespace(string $modelName): a
{
// Matches <DomainNamespace>\{domain}\<ModelNamespace>\{model} and extracts domain and model
// For example: Domain\Invoicing\Models\Invoice gives ['domain' => 'Invoicing', 'model' => 'Invoice']
$regex = '/'.DomainResolver::getConfiguredDomainNamespace().'\\\\(?<domain>.+)\\\\'.$this->config['namespaces.models'].'\\\\(?<model>.+)/';
$regex = '/'.DomainResolver::domainRootNamespace().'\\\\(?<domain>.+)\\\\'.$this->config['namespaces.models'].'\\\\(?<model>.+)/';

if (preg_match($regex, $modelName, $matches, PREG_OFFSET_CAPTURE, 0)) {
return [
Expand All @@ -153,4 +154,22 @@ protected function extractDomainAndModelFromModelNamespace(string $modelName): a

return [];
}

protected function remember($fileName, $callback)
{
$cacheFilePath = base_path($this->cacheDirectory.'/'.$fileName.'.php');

$data = file_exists($cacheFilePath) ? include $cacheFilePath : null;

if (is_null($data)) {
$data = $callback();

file_put_contents(
$cacheFilePath,
'<?php ' . PHP_EOL . 'return ' . var_export($data, true) . ';'
);
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/Support/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function join(...$parts)
public static function filePathToNamespace(string $path, string $namespacePath, string $namespace): string
{
return str_replace(
[base_path().$namespacePath, '/', '.php'],
[base_path().'/'.$namespacePath, '/', '.php'],
[$namespace, '\\', ''],
$path
);
Expand Down

0 comments on commit edf44d6

Please sign in to comment.