|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TailwindComponents\LaravelPreset; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Illuminate\Container\Container; |
| 8 | +use Illuminate\Filesystem\Filesystem; |
| 9 | +use Laravel\Ui\Presets\Preset; |
| 10 | +use Symfony\Component\Finder\SplFileInfo; |
| 11 | + |
| 12 | +class LaravelPreset extends Preset |
| 13 | +{ |
| 14 | + public static function install() |
| 15 | + { |
| 16 | + static::updatePackages(); |
| 17 | + static::updateStyles(); |
| 18 | + static::updateBootstrapping(); |
| 19 | + static::updatePagination(); |
| 20 | + static::removeNodeModules(); |
| 21 | + } |
| 22 | + |
| 23 | + public static function installAuth() |
| 24 | + { |
| 25 | + static::scaffoldController(); |
| 26 | + static::scaffoldAuth(); |
| 27 | + } |
| 28 | + |
| 29 | + protected static function updatePackageArray(array $packages) |
| 30 | + { |
| 31 | + return array_merge([ |
| 32 | + 'laravel-mix' => '^5.0.1', |
| 33 | + 'tailwindcss' => '^1.4', |
| 34 | + '@tailwindcss/custom-forms' => '^0.2', |
| 35 | + ], Arr::except($packages, [ |
| 36 | + 'bootstrap', |
| 37 | + 'bootstrap-sass', |
| 38 | + 'popper.js', |
| 39 | + 'laravel-mix', |
| 40 | + 'jquery', |
| 41 | + ])); |
| 42 | + } |
| 43 | + |
| 44 | + protected static function updateStyles() |
| 45 | + { |
| 46 | + tap(new Filesystem, function ($filesystem) { |
| 47 | + $filesystem->deleteDirectory(resource_path('sass')); |
| 48 | + $filesystem->delete(public_path('js/app.js')); |
| 49 | + $filesystem->delete(public_path('css/app.css')); |
| 50 | + |
| 51 | + if (! $filesystem->isDirectory($directory = resource_path('css'))) { |
| 52 | + $filesystem->makeDirectory($directory, 0755, true); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + copy(__DIR__.'/stubs/resources/css/app.css', resource_path('css/app.css')); |
| 57 | + } |
| 58 | + |
| 59 | + protected static function updateBootstrapping() |
| 60 | + { |
| 61 | + copy(__DIR__.'/stubs/tailwind.config.js', base_path('tailwind.config.js')); |
| 62 | + |
| 63 | + copy(__DIR__.'/stubs/webpack.mix.js', base_path('webpack.mix.js')); |
| 64 | + |
| 65 | + copy(__DIR__.'/stubs/resources/js/app.js', resource_path('js/app.js')); |
| 66 | + |
| 67 | + copy(__DIR__.'/stubs/resources/js/bootstrap.js', resource_path('js/bootstrap.js')); |
| 68 | + |
| 69 | + (new Filesystem)->copyDirectory(__DIR__.'/stubs/resources/js/components', resource_path('js/components')); |
| 70 | + } |
| 71 | + |
| 72 | + protected static function updatePagination() |
| 73 | + { |
| 74 | + (new Filesystem)->delete(resource_path('views/vendor/paginate')); |
| 75 | + |
| 76 | + (new Filesystem)->copyDirectory(__DIR__.'/stubs/resources/views/vendor/pagination', resource_path('views/vendor/pagination')); |
| 77 | + } |
| 78 | + |
| 79 | + protected static function scaffoldController() |
| 80 | + { |
| 81 | + if (! is_dir($directory = app_path('Http/Controllers/Auth'))) { |
| 82 | + mkdir($directory, 0755, true); |
| 83 | + } |
| 84 | + |
| 85 | + $filesystem = new Filesystem; |
| 86 | + |
| 87 | + collect($filesystem->allFiles(base_path('vendor/laravel/ui/stubs/Auth'))) |
| 88 | + ->each(function (SplFileInfo $file) use ($filesystem) { |
| 89 | + $filesystem->copy( |
| 90 | + $file->getPathname(), |
| 91 | + app_path('Http/Controllers/Auth/'.Str::replaceLast('.stub', '.php', $file->getFilename())) |
| 92 | + ); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + protected static function scaffoldAuth() |
| 97 | + { |
| 98 | + file_put_contents(app_path('Http/Controllers/HomeController.php'), static::compileControllerStub()); |
| 99 | + |
| 100 | + file_put_contents( |
| 101 | + base_path('routes/web.php'), |
| 102 | + "Auth::routes();\n\nRoute::get('/home', 'HomeController@index')->name('home');\n\n", |
| 103 | + FILE_APPEND |
| 104 | + ); |
| 105 | + |
| 106 | + tap(new Filesystem, function ($filesystem) { |
| 107 | + $filesystem->copyDirectory(__DIR__.'/stubs/resources/views', resource_path('views')); |
| 108 | + |
| 109 | + collect($filesystem->allFiles(base_path('vendor/laravel/ui/stubs/migrations'))) |
| 110 | + ->each(function (SplFileInfo $file) use ($filesystem) { |
| 111 | + $filesystem->copy( |
| 112 | + $file->getPathname(), |
| 113 | + database_path('migrations/'.$file->getFilename()) |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + protected static function compileControllerStub() |
| 120 | + { |
| 121 | + return str_replace( |
| 122 | + '{{namespace}}', |
| 123 | + Container::getInstance()->getNamespace(), |
| 124 | + file_get_contents(__DIR__.'/stubs/controllers/HomeController.stub') |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments