|
| 1 | +<?php |
| 2 | +namespace Cankod\Theme\Commands; |
| 3 | + |
| 4 | +use Illuminate\Console\Command; |
| 5 | +USE Illuminate\Support\Facades\File; |
| 6 | +use Illuminate\Support\Facades\Validator; |
| 7 | + |
| 8 | +class ThemeGeneratorCommand extends Command { |
| 9 | + |
| 10 | + protected $signature = 'theme:generate'; |
| 11 | + |
| 12 | + protected $description = 'Generate New Theme'; |
| 13 | + |
| 14 | + protected $config; |
| 15 | + |
| 16 | + |
| 17 | + public function __construct() |
| 18 | + { |
| 19 | + $this->config = config('theme'); |
| 20 | + parent::__construct(); |
| 21 | + } |
| 22 | + |
| 23 | + public function handle() |
| 24 | + { |
| 25 | + $name = $this->ask('Theme Name'); |
| 26 | + |
| 27 | + $validator = Validator::make([ |
| 28 | + 'name' => $name, |
| 29 | + ], [ |
| 30 | + 'name' => ['required'], |
| 31 | + ]); |
| 32 | + |
| 33 | + if ($validator->fails()) { |
| 34 | + $this->info('Failed to create theme. You must enter the following fields:'); |
| 35 | + |
| 36 | + foreach ($validator->errors()->all() as $error) { |
| 37 | + $this->error($error); |
| 38 | + } |
| 39 | + return $this->handle(); |
| 40 | + } |
| 41 | + |
| 42 | + if (!File::exists($this->getDirectory('resources'))) |
| 43 | + { |
| 44 | + File::makeDirectory($this->getDirectory('resources')); |
| 45 | + } |
| 46 | + |
| 47 | + if (!File::exists($this->getDirectory('resources',$name))) |
| 48 | + { |
| 49 | + // Themes altına klasör oluştur. |
| 50 | + File::makeDirectory($this->getDirectory('resources',$name)); |
| 51 | + File::makeDirectory($this->getDirectory('resources',$name.'/views')); |
| 52 | + |
| 53 | + if ($this->confirm('Create a default theme folder structure?',true)) { |
| 54 | + $this->template($name); |
| 55 | + } |
| 56 | + if ($this->confirm('Create a default asset folder structure?',true)) { |
| 57 | + $this->assets($name); |
| 58 | + } |
| 59 | + |
| 60 | + $this->info("Theme Generator Successful. Now add your default theme from the config/theme settings."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $this->error('Theme Folder Previously Created!'); |
| 65 | + |
| 66 | + return $this->handle(); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + protected function template($themeName) |
| 71 | + { |
| 72 | + $this->setFolder('resources',$themeName.'/views/'.$this->config['views_folder']['layout']); |
| 73 | + $this->setFolder('resources',$themeName.'/views/'.$this->config['views_folder']['component']); |
| 74 | + |
| 75 | + $layoutTemplate = str_replace( |
| 76 | + [ |
| 77 | + '{{header}}', |
| 78 | + '{{footer}}' |
| 79 | + ], |
| 80 | + [ |
| 81 | + $this->config['views_folder']['component'].'.'.$this->config['views_blade']['header'], |
| 82 | + $this->config['views_folder']['component'].'.'.$this->config['views_blade']['footer'] |
| 83 | + ], |
| 84 | + $this->getStub('layout') |
| 85 | + ); |
| 86 | + |
| 87 | + $indexTemplate = str_replace( |
| 88 | + [ |
| 89 | + '{{layoutName}}' |
| 90 | + ], |
| 91 | + [ |
| 92 | + $this->config['views_folder']['layout'].'.'.$this->config['views_blade']['layout'], |
| 93 | + ], |
| 94 | + $this->getStub('index') |
| 95 | + ); |
| 96 | + |
| 97 | + $this->setFile('resources',$themeName.'/views/'.$this->config['views_folder']['layout'],$this->config['views_blade']['layout'].'.blade.php',$layoutTemplate); |
| 98 | + $this->setFile('resources',$themeName.'/views/'.$this->config['views_folder']['component'],$this->config['views_blade']['header'].'.blade.php'); |
| 99 | + $this->setFile('resources',$themeName.'/views/'.$this->config['views_folder']['component'],$this->config['views_blade']['footer'].'.blade.php'); |
| 100 | + $this->setFile('resources',$themeName.'/views/',$this->config['views_blade']['index'].'.blade.php',$indexTemplate); |
| 101 | + |
| 102 | + $this->info("Theme view files created!"); |
| 103 | + } |
| 104 | + |
| 105 | + protected function assets($themeName) |
| 106 | + { |
| 107 | + |
| 108 | + if (!File::exists($this->getDirectory('public'))) |
| 109 | + { |
| 110 | + // Default Asset Theme Folder Create |
| 111 | + $this->setFolder('public'); |
| 112 | + } |
| 113 | + |
| 114 | + if (!File::exists($this->getDirectory('public',$themeName))) |
| 115 | + { |
| 116 | + $this->setFolder('public',$themeName); |
| 117 | + $this->setFolder('public',$themeName.'/css'); |
| 118 | + $this->setFolder('public',$themeName.'/js'); |
| 119 | + |
| 120 | + $this->setFile('public',$themeName.'/css','app.css'); |
| 121 | + $this->setFile('public',$themeName.'/js','app.js'); |
| 122 | + |
| 123 | + $this->info("Theme asset files created!"); |
| 124 | + |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + protected function getStub($fileName) |
| 131 | + { |
| 132 | + return File::get(__DIR__.'/../Template/'.$fileName.'.stub'); |
| 133 | + } |
| 134 | + |
| 135 | + protected function getDirectory($folder,$path = NULL) |
| 136 | + { |
| 137 | + if ($folder === 'resources') |
| 138 | + { |
| 139 | + if ($path === NULL) |
| 140 | + { |
| 141 | + return resource_path($this->config['theme_path']); |
| 142 | + } |
| 143 | + return resource_path($this->config['theme_path'].'/'.$path); |
| 144 | + } |
| 145 | + |
| 146 | + if ($folder === 'public') |
| 147 | + { |
| 148 | + if ($path === NULL) |
| 149 | + { |
| 150 | + return public_path($this->config['asset_path']); |
| 151 | + } |
| 152 | + return public_path($this->config['asset_path'].'/'.$path); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + protected function setFile($directory,$folder,$file,$content = NULL) |
| 158 | + { |
| 159 | + if ($directory === 'resources') |
| 160 | + { |
| 161 | + return File::put($this->getDirectory('resources',$folder).'/'.$file,$content); |
| 162 | + } |
| 163 | + if ($directory === 'public') |
| 164 | + { |
| 165 | + return File::put($this->getDirectory('public',$folder).'/'.$file,$content); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + protected function setFolder($directory,$path = null) |
| 170 | + { |
| 171 | + if ($directory === 'resources') |
| 172 | + { |
| 173 | + if ($path == null) |
| 174 | + { |
| 175 | + return File::makeDirectory(resource_path($this->config['theme_path'])); |
| 176 | + } |
| 177 | + return File::makeDirectory(resource_path($this->config['theme_path']).'/'.$path); |
| 178 | + } |
| 179 | + if ($directory === 'public') |
| 180 | + { |
| 181 | + if ($path == null) |
| 182 | + { |
| 183 | + return File::makeDirectory(public_path($this->config['theme_path'])); |
| 184 | + } |
| 185 | + return File::makeDirectory(public_path($this->config['theme_path']).'/'.$path); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments