Skip to content

Commit 1ffa99c

Browse files
author
Sean Bethel
committed
fix installation; use RS layout as main layout
1 parent 33dcf35 commit 1ffa99c

File tree

4 files changed

+79
-19
lines changed

4 files changed

+79
-19
lines changed

src/ReactSyncPreset.php

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
use Illuminate\Filesystem\Filesystem;
77
use Laravel\Ui\Presets\Preset;
88

9+
use Laravel\Ui\UiCommand;
10+
use Laravel\Ui\AuthCommand;
11+
12+
13+
use Schema;
14+
915
class ReactSyncPreset extends Preset
1016
{
1117

@@ -15,38 +21,55 @@ class ReactSyncPreset extends Preset
1521

1622
static $start_added = true;
1723

24+
static $command;
25+
1826
private function getJsPath(){
19-
return is_dir(Paths::resource_path('js')) ? Paths::resource_path('js') : Paths::resource_path('assets/js');
27+
return is_dir(Paths::resource_path('js')) ? Paths::resource_path('js') : Paths::resource_path('assets/js');
2028
}
2129

30+
31+
public static function install_auth(AuthCommand $command){
32+
static::$command = $command;
33+
$command->call('ui:auth');
34+
if ($command->confirm('Would you like to use the React Sync layout in place of the default layout view template?', 'yes')) {
35+
copy(__DIR__.'/views/layout.blade.php', Paths::resource_path("views/layouts/app.blade.php"));
36+
}
37+
38+
39+
40+
}
41+
2242
/**
2343
* Install the preset.
2444
*
2545
* @return void
2646
*/
27-
public static function install($command)
47+
public static function install(UiCommand $command)
2848
{
49+
static::$command = $command;
2950
static::preflight($command);
3051

31-
3252
static::ensurePagesModelsDirectoriesExist();
3353
static::ensureComponentDirectoryExists();
3454
static::updatePackages();
3555
static::updateWebpackConfiguration();
3656
static::updateBootstrapping();
3757
static::updateComponent();
3858
static::removeNodeModules();
39-
\Artisan::call('ui:auth');
40-
if(self::$include_example){
41-
\Artisan::call('make:react_model Example');
42-
\Artisan::call('make:react_page Example');
59+
60+
$command->call('ui:auth', ['type' => 'react-sync']);
61+
// dd('sdf');
62+
63+
if(static::$include_example){
64+
$command->call('make:react_model', ['name' => 'Example']);
65+
$command->call('make:react_page', ['name' => 'Example']);
4366
}
4467

4568
static::addStartCommand();
4669

4770
$command->info('ReactSync scaffolding installed successfully.');
4871
$command->comment('Please run "npm install && npm run dev" to compile your fresh scaffolding.');
49-
if(self::$start_added){
72+
if(static::$start_added){
5073
$command->comment('We\'ve added a "start" command to the scripts section of package.json:');
5174
$command->comment('');
5275
$command->comment('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ');
@@ -78,19 +101,31 @@ public static function install($command)
78101

79102

80103
public static function preflight($command){
104+
try {
105+
\DB::connection()->getPdo();
106+
} catch (\Exception $e) {
107+
echo PHP_EOL;
108+
$command->error("Your database configuration does not appear to be configured yet. Afterwards, you will need to run 'php artisan react_sync:all' manually to complete installation.");
109+
echo PHP_EOL;
110+
111+
}
112+
113+
81114
$command->info("You are about to install the Laravel ReactSync preset");
82115
if (!$command->confirm('Would you like to continue?', 'yes')) {
83116
exit('Cancelled' . PHP_EOL);
84117
}
85118

119+
120+
86121
$command->info("The following questions will help get the preset configured for your specific requirements.");
87122

88123
if ($command->confirm('Would you like to include the Bootstrap framework?', 'yes')) {
89-
self::$include_bootstrap = true;
124+
static::$include_bootstrap = true;
90125
}
91126

92127
if ($command->confirm('Would you like to include an example Page and Model component to get started?', 'yes')) {
93-
self::$include_example = true;
128+
static::$include_example = true;
94129
}
95130

96131

@@ -108,7 +143,7 @@ public static function addStartCommand(){
108143
$package_json['scripts']['start'] = "php artisan react_sync:all && npm run watch";
109144
}
110145
else{
111-
self::$start_added = false;
146+
static::$start_added = false;
112147
}
113148

114149
file_put_contents(
@@ -176,6 +211,16 @@ protected static function updatePackageArray(array $packages)
176211
return $packages;
177212
}
178213

214+
protected static function confirm_copy($from, $to){
215+
if (file_exists($from)) {
216+
if (! static::$command->confirm("The file [{$from}] already exists. Do you want to replace it?")) {
217+
return;
218+
}
219+
}
220+
221+
copy($from, $to);
222+
}
223+
179224
/**
180225
* Update the Webpack configuration.
181226
*
@@ -216,7 +261,7 @@ protected static function updateBootstrapping()
216261
{
217262
$js_path = is_dir(Paths::resource_path('js')) ? 'js' : 'assets/js';
218263
copy(__DIR__.'/react-sync-stubs/app.js', Paths::resource_path("$js_path/app.js"));
219-
if(self::$include_bootstrap){
264+
if(static::$include_bootstrap){
220265
copy(__DIR__.'/react-sync-stubs/app.scss', Paths::resource_path("sass/app.scss"));
221266
copy(__DIR__.'/react-sync-stubs/_variables.scss', Paths::resource_path("sass/_variables.scss"));
222267
}

src/ReactSyncable.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ public function syncable_boot()
119119
{
120120
$this->loadRoutesFrom(__DIR__.'/routes.php');
121121

122-
$this->loadRoutesFrom(__DIR__.'/console.php');
122+
if ($this->app->runningInConsole()) {
123+
$this->loadRoutesFrom(__DIR__.'/console.php');
124+
}
123125

124126
$this->loadViewsFrom(__DIR__.'/views', 'react_sync');
125127

@@ -134,8 +136,14 @@ public function syncable_boot()
134136

135137
// Load this into the `ui` Artisan command as the type: `react-sync`
136138

137-
UiCommand::macro('react-sync', function (UiCommand $command) {
138-
ReactSyncPreset::install($command);
139+
UiCommand::macro('react-sync', function ($command) {
140+
if($command instanceof UiCommand){
141+
ReactSyncPreset::install($command);
142+
}
143+
else{
144+
ReactSyncPreset::install_auth($command);
145+
}
146+
139147
});
140148

141149
}

src/helpers.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,21 @@ function returnFunctionText(ReflectionMethod $method){
9090
if(!function_exists('get_schema')){
9191
function get_schema($model = null){
9292
$connection = Schema::getConnection();
93+
try {
94+
\DB::connection()->getPdo();
95+
} catch (\Exception $e) {
96+
97+
return;
98+
}
99+
93100

94101
if($model === null) die("\npass in a model instance!\n\n");
95102

96103
$table = $model->getTable();
97104
$primary_key = $model->getKeyName();
98105

99106
$attrs = Schema::getColumnListing($model->getTable());
100-
107+
101108
$attrs = collect($attrs)->map(function($column) use($table, $connection, $primary_key){
102109
$column_definition = $connection->getDoctrineColumn($table, $column)->toArray();
103110
$column_definition['type'] = $column_definition['type']->getName();
@@ -114,7 +121,7 @@ function get_schema($model = null){
114121
$appended_attrs = coerceAsArray($model)->only(['with', 'appends'])->flatten();
115122

116123
$reflection = new ReflectionClass($model);
117-
124+
118125
$reflected_relations = new ReflectionClass(Illuminate\Database\Eloquent\Concerns\HasRelationships::class);
119126
$reflected_relations = collect($reflected_relations->getMethods())->map->getName();
120127

@@ -126,7 +133,7 @@ function get_schema($model = null){
126133
$isrel = !!$reflected_relations->first(function($m) use($function_text){
127134
return str_contains($function_text, '->' . $m);
128135
});
129-
136+
130137
return $isrel;
131138
}
132139
});

src/views/layout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<!-- Styles -->
2020
<link href="{{ asset('css/app.css') }}?v={{env('APP_VERSION', 9999999)}}" rel="stylesheet">
2121
</head>
22-
<body class="{{$bg_class ?? ''}} {{$page_name_class}}">
22+
<body class="{{$bg_class ?? ''}} {{$page_name_class ?? ''}}">
2323
<div id="app">
2424
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
2525
<div class="container">

0 commit comments

Comments
 (0)