Skip to content

Commit fa438b2

Browse files
committed
Fixed bug in Lens:make command that affects domain spaced set ups
1 parent bf3d472 commit fa438b2

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

resources/stubs/IndexedBase.php.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\Models\Indexes;
3+
namespace {{ namespace }}\Indexes;
44

5-
use App\Models\{{ model }};
5+
use {{ namespace }}\{{ model }};
66
use PDPhilip\ElasticLens\Builder\IndexBuilder;
77
use PDPhilip\ElasticLens\Builder\IndexField;
88
use PDPhilip\ElasticLens\IndexModel;

src/Commands/LensMakeCommand.php

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,52 @@ public function handle(): int
3434
$modelFound = null;
3535
$indexedModel = null;
3636
$namespaces = config('elasticlens.namespaces');
37+
$paths = config('elasticlens.index_paths');
38+
39+
$config = [
40+
'model' => [
41+
'name' => '',
42+
'namespace' => '',
43+
'full' => '',
44+
],
45+
'index' => [
46+
'name' => '',
47+
'namespace' => '',
48+
'full' => '',
49+
'path' => '',
50+
],
51+
52+
];
53+
3754
$notFound = [];
3855
foreach ($namespaces as $modelNamespace => $indexNameSpace) {
3956
$modelCheck = $modelNamespace.'\\'.$model;
4057
if ($this->class_exists_case_sensitive($modelCheck)) {
4158
$modelFound = $modelCheck;
42-
$indexedModel = $indexNameSpace.'\\Indexed'.$model;
59+
$config['model']['name'] = $model;
60+
$config['model']['namespace'] = $modelNamespace;
61+
$config['model']['full'] = $modelCheck;
62+
$config['index']['name'] = 'Indexed'.$model;
63+
$config['index']['namespace'] = $indexNameSpace;
64+
$config['index']['full'] = $indexNameSpace.'\\'.$config['index']['name'];
65+
66+
$path = array_search($indexNameSpace, $paths);
67+
if (! $path) {
68+
$this->omni->statusError('ERROR', 'Path for namespace '.$indexNameSpace.' not found', [
69+
'Namespace found: '.$indexNameSpace,
70+
'Check config("elasticlens.index_paths") for the correct {path} => \''.$indexNameSpace.'\'',
71+
]);
72+
$this->newLine();
73+
74+
return self::FAILURE;
75+
}
76+
$config['index']['path'] = $path;
4377
break;
4478
} else {
4579
$notFound[] = $modelCheck;
4680
}
4781
}
82+
4883
if (! $modelFound) {
4984
foreach ($notFound as $modelCheck) {
5085
$this->omni->statusError('ERROR', 'Base Model ('.$model.') was not found at: '.$modelCheck);
@@ -53,28 +88,26 @@ public function handle(): int
5388

5489
return self::FAILURE;
5590
}
56-
if ($this->class_exists_case_sensitive($indexedModel)) {
57-
$this->omni->statusError('ERROR', 'Indexed Model (for '.$model.' Model) already exists at: '.$indexedModel);
91+
if ($this->class_exists_case_sensitive($config['index']['full'])) {
92+
$this->omni->statusError('ERROR', 'Indexed Model (for '.$model.' Model) already exists at: '.$config['index']['full']);
5893

5994
return self::FAILURE;
6095
}
61-
// Set the fully qualified class name for the new indexed model
62-
$name = $this->qualifyClass($indexedModel);
6396

64-
// Get the destination path for the generated file
65-
$path = $this->getPath($name);
97+
$path = $config['index']['path'].$config['index']['name'];
6698

99+
$finalPath = $this->getPath($path);
67100
// Make sure the directory exists
68-
$this->makeDirectory($path);
101+
$this->makeDirectory($finalPath);
69102

70103
// Get the stub file contents
71104
$stub = $this->files->get($this->getStub());
72-
73105
// Replace the stub variables
74-
$stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
106+
$stub = $this->replaceNamespaceCustom($stub, $config['model']['namespace']);
107+
$stub = $this->replaceModel($stub, $config['model']['name']);
75108

76109
// Write the file to disk
77-
$this->files->put($path, $stub);
110+
$this->files->put($finalPath, $stub);
78111

79112
$this->omni->statusSuccess('SUCCESS', 'Indexed Model (for '.$model.' Model) created at: '.$indexedModel);
80113
$this->omni->statusInfo('1', 'Add the Indexable trait to your <span class="text-sky-500">'.$model.'</span> model');
@@ -98,11 +131,21 @@ protected function getStub(): string
98131
return $stubPath;
99132
}
100133

101-
public function replaceClass($stub, $name): string
134+
protected function getPath($name)
102135
{
103-
$stub = parent::replaceClass($stub, $name);
136+
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
137+
138+
return $this->laravel['path.base'].'/'.str_replace('\\', '/', $name).'.php';
139+
}
104140

105-
return str_replace('{{ model }}', $this->argument('model'), $stub);
141+
public function replaceNamespaceCustom($stub, $namespace): string
142+
{
143+
return str_replace('{{ namespace }}', $namespace, $stub);
144+
}
145+
146+
public function replaceModel($stub, $name): string
147+
{
148+
return str_replace('{{ model }}', $name, $stub);
106149
}
107150

108151
public function class_exists_case_sensitive(string $class_name): bool

0 commit comments

Comments
 (0)