-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f5c2fb
commit ff33e87
Showing
19 changed files
with
464 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Models\Generators; | ||
|
||
use DragonCode\Support\Facades\Filesystem\File; | ||
use DragonCode\Support\Facades\Helpers\Str; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str as IS; | ||
use LaravelLang\Config\Facades\Config; | ||
use LaravelLang\Models\Eloquent\Translation; | ||
|
||
abstract class Generator | ||
{ | ||
protected string $stub; | ||
|
||
protected array $registry = []; | ||
|
||
abstract protected function data(): array; | ||
|
||
abstract protected function filename(): string; | ||
|
||
public function __construct( | ||
protected string $model, | ||
protected array $columns | ||
) {} | ||
|
||
public static function of(string $model, array $columns = []): static | ||
{ | ||
return new static($model, $columns); | ||
} | ||
|
||
public function generate(): void | ||
{ | ||
$this->store($this->filename(), $this->make()); | ||
} | ||
|
||
protected function make(): string | ||
{ | ||
return Str::of($this->template()) | ||
->replaceFormat($this->resolveData(), '{{%s}}') | ||
->toString(); | ||
} | ||
|
||
protected function baseData(): array | ||
{ | ||
return [ | ||
'fqn' => $this->getFqn(), | ||
'namespace' => $this->getNamespace(), | ||
'model' => $this->getModel(), | ||
]; | ||
} | ||
|
||
protected function resolveData(): array | ||
{ | ||
return collect($this->data()) | ||
->map(fn (mixed $value) => is_array($value) ? implode(PHP_EOL, $value) : $value) | ||
->merge($this->baseData()) | ||
->all(); | ||
} | ||
|
||
protected function store(string $path, string $content): void | ||
{ | ||
File::store($path, $content); | ||
} | ||
|
||
protected function getFqn(): string | ||
{ | ||
return ltrim($this->model, '\\'); | ||
} | ||
|
||
protected function getNamespace(): string | ||
{ | ||
return IS::of($this->model) | ||
->ltrim('\\') | ||
->beforeLast('\\') | ||
->toString(); | ||
} | ||
|
||
protected function getModel(): string | ||
{ | ||
return IS::afterLast($this->model, '\\'); | ||
} | ||
|
||
protected function baseModel(): Model | ||
{ | ||
return $this->registry[$this->model] ??= new $this->model(); | ||
} | ||
|
||
protected function translationModel(): Translation | ||
{ | ||
$model = $this->translationModelNamespace(); | ||
|
||
return $this->registry[$model] ??= new $model(); | ||
} | ||
|
||
protected function translationModelNamespace(): string | ||
{ | ||
return $this->model . $this->modelSuffix(); | ||
} | ||
|
||
protected function modelSuffix(): string | ||
{ | ||
return Config::shared()->models->suffix; | ||
} | ||
|
||
protected function template(): string | ||
{ | ||
return file_get_contents($this->stub); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Models\Generators; | ||
|
||
use Illuminate\Support\Str; | ||
use LaravelLang\Config\Facades\Config; | ||
|
||
class HelperGenerator extends Generator | ||
{ | ||
protected string $stub = __DIR__ . '/../../stubs/helper.stub'; | ||
|
||
protected string $properties = ' * @property string|null $%s'; | ||
|
||
protected function data(): array | ||
{ | ||
return [ | ||
'hash' => $this->getHash(), | ||
'properties' => $this->getProperties(), | ||
|
||
'translationNamespace' => $this->translationModelNamespace(), | ||
'translationModel' => $this->getTranslationModel(), | ||
]; | ||
} | ||
|
||
protected function filename(): string | ||
{ | ||
return Config::shared()->models->helpers . '/_ide_helper_models_' . $this->getHash() . '.php'; | ||
} | ||
|
||
protected function getProperties(): array | ||
{ | ||
return array_map(fn (string $attribute) => sprintf($this->properties, $attribute), $this->getTranslatable()); | ||
} | ||
|
||
protected function getHash(): string | ||
{ | ||
return md5($this->model); | ||
} | ||
|
||
protected function getTranslatable(): array | ||
{ | ||
return $this->translationModel()->translatable(); | ||
} | ||
|
||
protected function getTranslationModel(): string | ||
{ | ||
return Str::afterLast($this->translationModelNamespace(), '\\'); | ||
} | ||
} |
Oops, something went wrong.