Skip to content

Commit a5401d6

Browse files
authored
Merge pull request #48
Add vendor publish as well as register make:fm-model command
2 parents 0a7519a + aa6af13 commit a5401d6

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Install `gearbox-solutions/eloquent-filemaker` in your project using Composer.
5959
```
6060
composer require gearbox-solutions/eloquent-filemaker
6161
```
62+
6263
# Usage
6364
With the package installed you can now have access to all the features of this package. There are a few different areas to configure.
6465

@@ -111,9 +112,36 @@ cache_driver=file
111112
## Model Classes
112113
Creating model classes is the easiest way to access your FileMaker data, and is the most Laravel-like way of doing things. Create a new model class and change the extension class from `Model` to `FMModel`. This class change enables you to use the features of this package with your models.
113114

115+
### Artisan make:model command
116+
You can use the default `php artisan make:model` command with a new `--filemaker` flag to make a new `FMModel` instead of the default `Model`. All options available to Laravel's native `make:model` command are still available for use.
117+
118+
```shell
119+
php artisan make:model MyNewModel --filemaker
120+
```
121+
122+
### Set FMModel as default with a model stub
123+
124+
If you would like all of your models to be published as FMModel by default so that you don't have to use `--filemaker` in your commands, you can use the following command to publish a model stub that will be used by `php artisan make:model` to set up new models.
125+
126+
```shell
127+
php artisan vendor:publish --tag=eloquent-filemaker-override-model
128+
```
129+
This publish will create a `/stubs/model.stub` that will be used by `php artisan make:model` to set up new models. You should use this on projects that will only have models backed by FileMaker.
130+
131+
If you want to customize the model stub ONLY for when the `---filemaker` flag is used, you can do so with the following command:
132+
133+
```shell
134+
php artisan vendor:publish --tag=eloquent-filemaker-stubs
135+
````
136+
or
137+
```shell
138+
php artisan vendor:publish --provider="GearboxSolutions\EloquentFileMaker\Providers\FileMakerConnectionServiceProvider"
139+
```
140+
This stub publish option is best if you are looking to have a mix of FileMaker backed models and another DB backed model.
141+
114142

115143

116-
#### Things that work
144+
### Things that work
117145

118146
The FMModel class extends the base Laravel Model class, and can be used very similarly. It supports many standard Eloquent query builder features for working with data, such as where(), find(), id(), orderBy(), delete(), save(), and many more!
119147

@@ -123,7 +151,7 @@ Our goal is to be able to use any of these Eloquent features which make sense, s
123151
124152
Be sure to read [Laravel's Eloquent Documentation](https://laravel.com/docs/8.x/eloquent) to see all the things the Eloquent Model class can do.
125153

126-
#### Things that don't work
154+
### Things that don't work
127155
Because this class extends Model, all of the regular eloquent methods may show as available in your IDE, but some don't make sense in the context of FileMaker's Data API and therefore don't do anything. Some examples of this would be mass updates or raw SQL queries.
128156
129157
### Setting a layout
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace GearboxSolutions\EloquentFileMaker\Commands;
4+
5+
use Illuminate\Foundation\Console\ModelMakeCommand as LaravelModelMakeCommand;
6+
use Symfony\Component\Console\Input\InputOption;
7+
8+
class FMModelMakeCommand extends LaravelModelMakeCommand
9+
{
10+
protected $name = 'make:model';
11+
12+
/**
13+
* The console command description.
14+
*
15+
* @var string
16+
*/
17+
protected $description = 'Create a new Eloquent FileMaker model class';
18+
19+
public function handle()
20+
{
21+
parent::handle();
22+
23+
$this->input->setOption('filemaker', true);
24+
}
25+
26+
public function getStub()
27+
{
28+
$stub = parent::getStub();
29+
30+
if ($this->option('filemaker')) {
31+
if ($this->option('pivot') || $this->option('morph-pivot')) {
32+
throw new \RuntimeException('This model type is not yet supported by Eloquent FileMaker.');
33+
}
34+
35+
$stub = $this->resolveFMStubPath('/stubs/fm.model.stub');
36+
}
37+
38+
return $stub;
39+
}
40+
41+
/**
42+
* Resolve the fully-qualified path to the stub.
43+
*
44+
* @param string $stub
45+
* @return string
46+
*/
47+
protected function resolveFMStubPath($stub)
48+
{
49+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
50+
? $customPath
51+
: __DIR__ . $stub;
52+
}
53+
54+
protected function getOptions()
55+
{
56+
return array_merge(parent::getOptions(), [
57+
['filemaker', null, InputOption::VALUE_NONE, 'Use the FileMaker stub instead of base model stub'],
58+
]);
59+
}
60+
}

src/Commands/stubs/fm.model.stub

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\FMModel;
7+
8+
class {{ class }} extends FMModel
9+
{
10+
use HasFactory;
11+
}

src/Providers/FileMakerConnectionServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GearboxSolutions\EloquentFileMaker\Providers;
44

5+
use GearboxSolutions\EloquentFileMaker\Commands\FMModelMakeCommand;
56
use GearboxSolutions\EloquentFileMaker\Middleware\EndSession;
67
use GearboxSolutions\EloquentFileMaker\Services\FileMakerConnection;
78
use Illuminate\Contracts\Http\Kernel;
@@ -38,6 +39,11 @@ public function register()
3839

3940
app('router')->aliasMiddleware('fm.end-session', EndSession::class);
4041

42+
if ($this->app->runningInConsole()) {
43+
$this->commands([
44+
FMModelMakeCommand::class,
45+
]);
46+
}
4147
}
4248

4349
/**
@@ -49,5 +55,16 @@ public function boot(Kernel $kernel)
4955
{
5056
// add the middleware to the global middleware so that we always end the FileMaker session
5157
$kernel->pushMiddleware(EndSession::class);
58+
59+
$this->publishes([
60+
__DIR__ . '/../config/eloquent-filemaker.php' => config_path('eloquent-filemaker.php'),
61+
], 'eloquent-filemaker-config');
62+
$this->publishes([
63+
__DIR__ . '/../Commands/stubs/fm.model.stub' => base_path('stubs/model.stub'),
64+
], 'eloquent-filemaker-override-model');
65+
66+
$this->publishes([
67+
__DIR__ . '/../Commands/stubs/fm.model.stub' => base_path('stubs/fm.model.stub'),
68+
], 'eloquent-filemaker-stubs');
5269
}
5370
}

0 commit comments

Comments
 (0)