Skip to content

Commit

Permalink
Merge pull request #48
Browse files Browse the repository at this point in the history
Add vendor publish as well as register make:fm-model command
  • Loading branch information
Smef authored Oct 11, 2024
2 parents 0a7519a + aa6af13 commit a5401d6
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Install `gearbox-solutions/eloquent-filemaker` in your project using Composer.
```
composer require gearbox-solutions/eloquent-filemaker
```

# Usage
With the package installed you can now have access to all the features of this package. There are a few different areas to configure.

Expand Down Expand Up @@ -111,9 +112,36 @@ cache_driver=file
## Model Classes
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.

### Artisan make:model command
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.

```shell
php artisan make:model MyNewModel --filemaker
```

### Set FMModel as default with a model stub

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.

```shell
php artisan vendor:publish --tag=eloquent-filemaker-override-model
```
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.

If you want to customize the model stub ONLY for when the `---filemaker` flag is used, you can do so with the following command:

```shell
php artisan vendor:publish --tag=eloquent-filemaker-stubs
````
or
```shell
php artisan vendor:publish --provider="GearboxSolutions\EloquentFileMaker\Providers\FileMakerConnectionServiceProvider"
```
This stub publish option is best if you are looking to have a mix of FileMaker backed models and another DB backed model.



#### Things that work
### Things that work

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!

Expand All @@ -123,7 +151,7 @@ Our goal is to be able to use any of these Eloquent features which make sense, s
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.

#### Things that don't work
### Things that don't work
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.
### Setting a layout
Expand Down
60 changes: 60 additions & 0 deletions src/Commands/FMModelMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace GearboxSolutions\EloquentFileMaker\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as LaravelModelMakeCommand;
use Symfony\Component\Console\Input\InputOption;

class FMModelMakeCommand extends LaravelModelMakeCommand
{
protected $name = 'make:model';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Eloquent FileMaker model class';

public function handle()
{
parent::handle();

$this->input->setOption('filemaker', true);
}

public function getStub()
{
$stub = parent::getStub();

if ($this->option('filemaker')) {
if ($this->option('pivot') || $this->option('morph-pivot')) {
throw new \RuntimeException('This model type is not yet supported by Eloquent FileMaker.');
}

$stub = $this->resolveFMStubPath('/stubs/fm.model.stub');
}

return $stub;
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveFMStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__ . $stub;
}

protected function getOptions()
{
return array_merge(parent::getOptions(), [
['filemaker', null, InputOption::VALUE_NONE, 'Use the FileMaker stub instead of base model stub'],
]);
}
}
11 changes: 11 additions & 0 deletions src/Commands/stubs/fm.model.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Factories\HasFactory;
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\FMModel;

class {{ class }} extends FMModel
{
use HasFactory;
}
17 changes: 17 additions & 0 deletions src/Providers/FileMakerConnectionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GearboxSolutions\EloquentFileMaker\Providers;

use GearboxSolutions\EloquentFileMaker\Commands\FMModelMakeCommand;
use GearboxSolutions\EloquentFileMaker\Middleware\EndSession;
use GearboxSolutions\EloquentFileMaker\Services\FileMakerConnection;
use Illuminate\Contracts\Http\Kernel;
Expand Down Expand Up @@ -38,6 +39,11 @@ public function register()

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

if ($this->app->runningInConsole()) {
$this->commands([
FMModelMakeCommand::class,
]);
}
}

/**
Expand All @@ -49,5 +55,16 @@ public function boot(Kernel $kernel)
{
// add the middleware to the global middleware so that we always end the FileMaker session
$kernel->pushMiddleware(EndSession::class);

$this->publishes([
__DIR__ . '/../config/eloquent-filemaker.php' => config_path('eloquent-filemaker.php'),
], 'eloquent-filemaker-config');
$this->publishes([
__DIR__ . '/../Commands/stubs/fm.model.stub' => base_path('stubs/model.stub'),
], 'eloquent-filemaker-override-model');

$this->publishes([
__DIR__ . '/../Commands/stubs/fm.model.stub' => base_path('stubs/fm.model.stub'),
], 'eloquent-filemaker-stubs');
}
}

0 comments on commit a5401d6

Please sign in to comment.