Mercurial > packages > magicforger
diff src/Replacer/Replacer.php @ 34:f65ab84ee47f default
merge with codex
| author | luka |
|---|---|
| date | Wed, 10 Sep 2025 21:00:47 -0400 |
| parents | 010ace248d14 |
| children | 55d2e5c5dad9 |
line wrap: on
line diff
--- a/src/Replacer/Replacer.php Sat Dec 02 10:20:32 2023 -0500 +++ b/src/Replacer/Replacer.php Wed Sep 10 21:00:47 2025 -0400 @@ -1,6 +1,6 @@ <?php -namespace Wizzard\MagicForger\Replacer; +namespace Wizard\MagicForger\Replacer; use Illuminate\Support\Str; @@ -9,27 +9,22 @@ /** * Prefix and Suffix for controller. * Usage is up to the user. - * - * @var string */ - protected $controller_prefix = ''; + protected string $controller_prefix = ''; /** * Prefix and Suffix for controller. * Usage is up to the user. - * - * @var string */ - protected $controller_suffix = 'Controller'; + protected string $controller_suffix = 'Controller'; /** * Finds all places in a string that could be replaced. - * Returns an array of all potential replacements as they - * appear in the target. + * Returns an array of all potential replacements as they appear in the target. */ public function get_all_keywords(string $target): array { - // find all the matches to our expected syntax + // find all matches to our expected syntax $matches = []; preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); // sort the array and return unique values @@ -38,46 +33,48 @@ return array_values(array_unique($matches[0])); } + /** + * Apply replacements to the target string. + */ public function apply_replacements(string $target): string { $inserts = $this->get_all_keywords($target); $available_replacements = $this->get_available_replacements(); - $target = str_replace( + return str_replace( array_keys($available_replacements), $available_replacements, $target ); - - return $target; } - public function get_available_replacements() + /** + * Get available replacements for string replacements. + */ + public function get_available_replacements(): array { $table_name = $this->getTableInput(); - $replacements = [ - '{{ class }}' => $this->getClassName($table_name), - '{{ controllerName }}' => $this->controller_name($table_name), - '{{ model }}' => $this->model_name($table_name), - '{{ modelVariable }}' => $this->model_variable($table_name), - '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), - '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), - '{{ requestUses }}' => $this->getRequestUses($table_name), - '{{ rootNamespace }}' => $this->getRootNamespace(), - '{{ storeRequest }}' => $this->store_request_name($table_name), - '{{ tableName }}' => $table_name, - '{{ updateRequest }}' => $this->update_request_name($table_name), + + return [ + '{{ class }}' => $this->getClassName($table_name), + '{{ controllerName }}' => $this->controller_name($table_name), + '{{ model }}' => $this->model_name($table_name), + '{{ modelVariable }}' => $this->model_variable($table_name), + '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), + '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), + '{{ requestUses }}' => $this->getRequestUses($table_name), + '{{ rootNamespace }}' => $this->getRootNamespace(), + '{{ storeRequest }}' => $this->store_request_name($table_name), + '{{ filterRequest }}' => $this->filter_request_name($table_name), + '{{ updateRequest }}' => $this->update_request_name($table_name), + '{{ tableName }}' => $table_name, ]; - - return $replacements; } - // ////////////////////////////////////////// - // Internals and Classes // - // ////////////////////////////////////////// + // Model and Controller Naming /** - * Model names are generated in uppercase first Camel case. + * Generate model name in Studly case. */ public function model_name(string $name): string { @@ -85,17 +82,15 @@ } /** - * Model variable is standardly just a singular version of the table name. + * Generate singular model variable name. */ public function model_variable(string $name): string { - /* return Str::singular($name); */ - return 'item'; + return Str::singular($name); } /** - * Controller names are generated in uppercase first Camel case - * and wrapped in the prefix and suffix. + * Generate controller name using prefix/suffix and studly case. */ public function controller_name(string $name): string { @@ -104,76 +99,192 @@ $this->controller_suffix; } + /** + * Generate the store request name. + */ public function store_request_name(string $name): string { return 'Store'.$this->model_name($name).'Request'; } + /** + * Generate the filter request name. + */ + public function filter_request_name(string $name): string + { + return 'Filter'.$this->model_name($name).'Request'; + } + + /** + * Generate the update request name. + */ public function update_request_name(string $name): string { return 'Update'.$this->model_name($name).'Request'; } - // ////////////////////////////////////////// - // Namespaces // - // ////////////////////////////////////////// + + /** + * Generate the index view name. + */ + public function index_view_name(string $name): string + { + return ''; + } + + /** + * Generate the create_edit view name. + */ + public function create_edit_view_name(string $name): string + { + return ''; + } - public function getRootNamespace() + /** + * Generate the show view name. + */ + public function show_view_name(string $name): string + { + return ''; + } + + + /** + * Generate route name in Studly case. + */ + public function routes_name(string $name): string + { + return Str::singular(Str::studly($name)); + } + + // Namespace Methods + // These methods handle the formation of various namespaces used within the replacements. + + /** + * Get the root namespace for the application. + */ + public function getRootNamespace(): string { return $this->laravel->getNamespace(); } - public function getModelNamespace(string $name = '') + /** + * Get the model namespace. + */ + public function getRouteNamespace(string $name = ''): string + { + return base_path() + . DIRECTORY_SEPARATOR . 'routes' + . DIRECTORY_SEPARATOR . 'resources' + ; + } + + /** + * Get the model namespace. + */ + public function getModelNamespace(string $name = ''): string { return $this->getRootNamespace().'Models'; } - public function getNamespacedModel(string $name = '') + /** + * Get the fully-qualified namespaced model class. + */ + public function getNamespacedModel(string $name = ''): string { return $this->getModelNamespace().'\\'.$this->model_name($name); } - public function getControllerNamespace(string $name = '') + /** + * Get the controller namespace. + */ + public function getControllerNamespace(string $name = ''): string { return $this->getRootNamespace().'Http\\Controllers'; } - public function getRequestNamespace(string $name) + /** + * Get the request namespace. + */ + public function getRequestNamespace(string $name): string { return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); } - public function getStoreRequestNamespace(string $name) + /** + * Get the store request namespace. + */ + public function getStoreRequestNamespace(string $name): string { return $this->getRequestNamespace($name); } - public function getUpdateRequestNamespace(string $name) + /** + * Get the filter request namespace. + */ + public function getFilterRequestNamespace(string $name): string + { + return $this->getRequestNamespace($name); + } + + /** + * Get the update request namespace. + */ + public function getUpdateRequestNamespace(string $name): string { return $this->getRequestNamespace($name); } - public function getRequestUses(string $name) + /** + * Get the view namespace. + */ + public function getViewNamespace(string $name): string + { + return $this->viewPath($name) . '\\'; + } + + /** + * Get the index view namespace. + */ + public function getIndexViewNamespace(string $name): string + { + return $this->getViewNamespace($name) . '\\'; + } + + /** + * Get the create_edit view namespace. + */ + public function getCreateEditViewNamespace(string $name): string + { + return $this->getViewNamespace($name) . '\\'; + } + + /** + * Get the show view namespace. + */ + public function getShowViewNamespace(string $name): string + { + return $this->getViewNamespace($name) . '\\'; + } + + + /** + * Get the request uses string for replacement. + */ + public function getRequestUses(string $name): string { return implode("\n", [ 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', + 'use '.$this->getRequestNamespace($name).'\\'.$this->filter_request_name($name).';', 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', ]); } - public function getRouteNamespace(string $name = '') - { - return $this->getRootNamespace().'Http\\Controllers'; - } - - // ////////////////////////////////////////// - // Language and Presentables // - // ////////////////////////////////////////// + // Text Manipulation /** - * Breaks up a string and makes it human readable. - * - * This function assumes that the inputted name is camel case + * Convert a string to a human-readable format. + * Assumes camel case input. */ public function human_readable(string $name): string { @@ -181,9 +292,8 @@ } /** - * Breaks up a string and makes it human readable and lowecase. - * - * This function assumes that the inputted name is camel case + * Convert a string to a lowercase human-readable format. + * Assumes camel case input. */ public function human_readable_lc(string $name): string {
