Mercurial > packages > magicforger
comparison src/Replacer.php @ 2:cf9993c5c7df
Updated .vimrc for some helper commands.
updated the Base Generator
Brought the controller generator into the package
created an example generator, but it needs some work.
| author | luka |
|---|---|
| date | Sun, 25 Jun 2023 14:45:15 -0400 |
| parents | ca36acd2bef2 |
| children | 6468684362c2 |
comparison
equal
deleted
inserted
replaced
| 1:ca36acd2bef2 | 2:cf9993c5c7df |
|---|---|
| 2 | 2 |
| 3 namespace Wizzard\MagicForger; | 3 namespace Wizzard\MagicForger; |
| 4 | 4 |
| 5 use Illuminate\Support\Str; | 5 use Illuminate\Support\Str; |
| 6 | 6 |
| 7 class Replacer { | 7 class Replacer |
| 8 | 8 { |
| 9 /** | 9 /** |
| 10 * Cached replacements for re-use. | 10 * Cached replacements for re-use. |
| 11 * | 11 * |
| 12 * @var array | 12 * @var array |
| 13 */ | 13 */ |
| 14 protected $replacement_cache = []; | 14 protected $replacement_cache = []; |
| 15 | 15 |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Prefix and Suffix for controller. | 18 * Prefix and Suffix for controller. |
| 19 * Usage is up to the user. | 19 * Usage is up to the user. |
| 20 * | 20 * |
| 21 * @var string | 21 * @var string |
| 22 */ | 22 */ |
| 23 protected $controller_prefix = ""; | 23 protected $controller_prefix = ""; |
| 24 | 24 |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Prefix and Suffix for controller. | 27 * Prefix and Suffix for controller. |
| 28 * Usage is up to the user. | 28 * Usage is up to the user. |
| 29 * | 29 * |
| 30 * @var string | 30 * @var string |
| 31 */ | 31 */ |
| 32 protected $controller_suffix = "Controller"; | 32 protected $controller_suffix = "Controller"; |
| 33 | 33 |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * The lowest level to show log outputs. | 36 * The lowest level to show log outputs. |
| 37 * | 37 * |
| 38 * @var int | 38 * @var int |
| 39 */ | 39 */ |
| 40 private $log_level = 1; | 40 private $log_level = 1; |
| 41 | 41 |
| 42 | 42 |
| 43 public function __construct() { | 43 public function __construct() |
| 44 /* parent::__construct(); */ | 44 { |
| 45 } | 45 /* parent::__construct(); */ |
| 46 } | |
| 46 | 47 |
| 47 /** | 48 /** |
| 48 * Model names are generated in uppercase first Camel case | 49 * Model names are generated in uppercase first Camel case |
| 49 */ | 50 */ |
| 50 public function model_name(string $name) : string { | 51 public function model_name(string $name): string |
| 51 return Str::singular(Str::studly($name)); | 52 { |
| 52 } | 53 return Str::singular(Str::studly($name)); |
| 54 } | |
| 53 | 55 |
| 54 /** | 56 /** |
| 55 * Controller names are generated in uppercase first Camel case | 57 * Controller names are generated in uppercase first Camel case |
| 56 * and wrapped in the prefix and suffix | 58 * and wrapped in the prefix and suffix |
| 57 */ | 59 */ |
| 58 public function controller_name(string $name) : string { | 60 public function controller_name(string $name): string |
| 59 return $this->controller_prefix . | 61 { |
| 60 $this->model_name($name) . | 62 return $this->controller_prefix . |
| 61 $this->controller_suffix; | 63 $this->model_name($name) . |
| 62 } | 64 $this->controller_suffix; |
| 65 } | |
| 63 | 66 |
| 64 /** | 67 /** |
| 65 * Breaks up a string and makes it human readable | 68 * Breaks up a string and makes it human readable |
| 66 * | 69 * |
| 67 * This function assumes that the inputted name is camel case | 70 * This function assumes that the inputted name is camel case |
| 68 */ | 71 */ |
| 69 public function human_readable(string $name) : string { | 72 public function human_readable(string $name): string |
| 70 return Str::title(Str::replace('_', ' ', $name)); | 73 { |
| 71 } | 74 return Str::title(Str::replace('_', ' ', $name)); |
| 75 } | |
| 72 | 76 |
| 73 /** | 77 /** |
| 74 * Breaks up a string and makes it human readable and lowecase | 78 * Breaks up a string and makes it human readable and lowecase |
| 75 * | 79 * |
| 76 * This function assumes that the inputted name is camel case | 80 * This function assumes that the inputted name is camel case |
| 77 */ | 81 */ |
| 78 public function human_readable_lc(string $name) : string { | 82 public function human_readable_lc(string $name): string |
| 79 return Str::lower($this->human_readable($name)); | 83 { |
| 80 } | 84 return Str::lower($this->human_readable($name)); |
| 85 } | |
| 81 | 86 |
| 82 /** | 87 /** |
| 83 * Outputs a log for the replacements based on log level. | 88 * Outputs a log for the replacements based on log level. |
| 84 */ | 89 */ |
| 85 public function log(string $str, int $log_level = 1) : void { | 90 public function log(string $str, int $log_level = 1): void |
| 91 { | |
| 86 | 92 |
| 87 if($this->log_level <= $log_level) { | 93 if($this->log_level <= $log_level) { |
| 88 print($str . "\n"); | 94 print($str . "\n"); |
| 89 } | 95 } |
| 90 | 96 |
| 91 } | 97 } |
| 92 } | 98 } |
