Skip to content

Commit 430d6d5

Browse files
committed
Make command
1 parent 6456c84 commit 430d6d5

6 files changed

Lines changed: 142 additions & 8 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Carbon\Carbon;
6+
use PDPhilip\Elasticsearch\Eloquent\HasMappingDefinition;
7+
use PDPhilip\Elasticsearch\Eloquent\Model as Eloquent;
8+
use PDPhilip\Elasticsearch\Schema\Blueprint;
9+
10+
/**
11+
* {{ namespace }}\{{ class }}
12+
*
13+
******Fields*******
14+
* @property string $id
15+
* @property Carbon $updated_at
16+
* @property Carbon $created_at
17+
*/
18+
class {{ class }} extends Eloquent implements HasMappingDefinition
19+
{
20+
protected $connection = 'elasticsearch';
21+
22+
public $timestamps = true;
23+
24+
public static function mappingDefinition(Blueprint $index): void
25+
{
26+
$index->date('updated_at');
27+
$index->date('created_at');
28+
}
29+
}

src/Commands/MakeCommand.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PDPhilip\Elasticsearch\Commands;
6+
7+
use Illuminate\Console\GeneratorCommand;
8+
use Illuminate\Support\Str;
9+
use OmniTerm\HasOmniTerm;
10+
11+
class MakeCommand extends GeneratorCommand
12+
{
13+
use HasOmniTerm;
14+
15+
protected $signature = 'elastic:make {name : The name of the Elasticsearch model}';
16+
17+
protected $description = 'Create a new Elasticsearch model';
18+
19+
protected $type = 'Model';
20+
21+
public function handle(): int
22+
{
23+
$name = Str::studly($this->argument('name'));
24+
25+
$namespace = $this->resolveNamespace($name);
26+
$class = class_basename(str_replace('/', '\\', $name));
27+
$fullClass = $namespace.'\\'.$class;
28+
$path = $this->getModelPath($name);
29+
30+
if (file_exists($path)) {
31+
$this->newLine();
32+
$this->omni->statusError('Model already exists', $fullClass);
33+
$this->newLine();
34+
35+
return self::FAILURE;
36+
}
37+
38+
$this->makeDirectory($path);
39+
40+
$stub = $this->buildStub($namespace, $class);
41+
$this->files->put($path, $stub);
42+
43+
$this->newLine();
44+
$this->omni->statusSuccess('Model created', $fullClass);
45+
$this->omni->tableRow('Path', str_replace(base_path().'/', '', $path));
46+
$this->newLine();
47+
48+
return self::SUCCESS;
49+
}
50+
51+
protected function getStub(): string
52+
{
53+
return __DIR__.'/../../resources/stubs/ElasticModel.php.stub';
54+
}
55+
56+
private function resolveNamespace(string $name): string
57+
{
58+
$parts = explode('/', str_replace('\\', '/', $name));
59+
array_pop($parts);
60+
61+
$sub = implode('\\', $parts);
62+
63+
return $sub
64+
? 'App\\Models\\'.$sub
65+
: 'App\\Models';
66+
}
67+
68+
private function getModelPath(string $name): string
69+
{
70+
$name = str_replace('\\', '/', $name);
71+
72+
return app_path('Models/'.$name.'.php');
73+
}
74+
75+
private function buildStub(string $namespace, string $class): string
76+
{
77+
$stub = $this->files->get($this->getStub());
78+
79+
return str_replace(
80+
['{{ namespace }}', '{{ class }}'],
81+
[$namespace, $class],
82+
$stub
83+
);
84+
}
85+
}

src/Commands/ReIndexCommand.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\Facades\DB;
1111
use OmniTerm\HasOmniTerm;
1212
use PDPhilip\Elasticsearch\Connection;
13+
use PDPhilip\Elasticsearch\Eloquent\HasMappingDefinition;
1314
use PDPhilip\Elasticsearch\Eloquent\Model;
1415
use PDPhilip\Elasticsearch\Schema\Blueprint;
1516
use PDPhilip\Elasticsearch\Schema\Builder as SchemaBuilder;
@@ -170,15 +171,21 @@ private function resolveModel(): ?Model
170171
return null;
171172
}
172173

173-
if (! method_exists($class, 'mappingDefinition')) {
174+
if (! is_subclass_of($class, HasMappingDefinition::class)) {
174175
$this->newLine();
175176
$this->omni->statusError('Missing mapping definition', $class, [
176-
'Add a public mappingDefinition() method to your model:',
177+
'Your model must implement HasMappingDefinition:',
177178
'',
178-
'public function mappingDefinition(Blueprint $index): void',
179+
'use PDPhilip\Elasticsearch\Eloquent\HasMappingDefinition;',
180+
'use PDPhilip\Elasticsearch\Schema\Blueprint;',
181+
'',
182+
'class YourModel extends Eloquent implements HasMappingDefinition',
179183
'{',
180-
' $index->keyword(\'status\');',
181-
' $index->geoPoint(\'location\');',
184+
' public static function mappingDefinition(Blueprint $index): void',
185+
' {',
186+
' $index->keyword(\'status\');',
187+
' $index->geoPoint(\'location\');',
188+
' }',
182189
'}',
183190
]);
184191
$this->newLine();
@@ -244,7 +251,7 @@ private function setupFromModel(Model $model): void
244251
$connection = DB::connection($this->connectionName);
245252
$this->schema = $connection->getSchemaBuilder();
246253

247-
$this->mappingDefinition = fn (Blueprint $index) => $model->mappingDefinition($index);
254+
$this->mappingDefinition = fn (Blueprint $index) => $model::mappingDefinition($index);
248255
}
249256

250257
// ======================================================================

src/ElasticServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function boot(): void
2323
Commands\IndicesCommand::class,
2424
Commands\ShowCommand::class,
2525
Commands\ReIndexCommand::class,
26+
Commands\MakeCommand::class,
2627
]);
2728
}
2829
}

src/Eloquent/ElasticsearchModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ protected function newBaseQueryBuilder()
130130
Model::OPTION_STORE_IDS => $this->storeIdInDocument,
131131
]);
132132

133-
if (method_exists($this, 'mappingDefinition')) {
133+
if ($this instanceof HasMappingDefinition) {
134134
$connection->registerMappingDefinition(
135135
$connection->getTablePrefix().$this->getTable(),
136-
fn ($index) => $this->mappingDefinition($index)
136+
fn ($index) => static::mappingDefinition($index)
137137
);
138138
}
139139

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PDPhilip\Elasticsearch\Eloquent;
6+
7+
use PDPhilip\Elasticsearch\Schema\Blueprint;
8+
9+
interface HasMappingDefinition
10+
{
11+
public static function mappingDefinition(Blueprint $index): void;
12+
}

0 commit comments

Comments
 (0)