Skip to content

Commit a0700cf

Browse files
authored
Merge pull request #36 from kirschbaum-development/feature/third-party-integrations
Feature/third party integrations
2 parents 5917758 + c8614f8 commit a0700cf

11 files changed

Lines changed: 205 additions & 5 deletions

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,56 @@ Occasionally you may want to require a child relationship during the creation of
7575
HasOne::make('Profile', 'profile', Profile::class)->inline()->requireChild(),
7676
```
7777

78+
## Support for Third Party Packages
79+
80+
We have included a simple way to address some issues regarding third party packages occasionally not working with Nova Inline Relationships. These packages occasionally handle how they return their subset of fields slightly different. We have a way of easily integrating functionality for these packages. This may not work across the board, but should allow for most third party packages.
81+
82+
You must publish the configs for this package with
83+
84+
```shell script
85+
php artisan vendor:publish
86+
```
87+
88+
This will publish a config file as `config/nova-inline-relationships.php`. Add your custom namespaced paths within the `third-party` array. For example:
89+
90+
```php
91+
'third-party' => [
92+
'App\Nova\ThirdPartyIntegrations',
93+
'KirschbaumDevelopment\NovaInlineRelationship\Integrations',
94+
]
95+
```
96+
97+
Create a new class inside that namespace that looks like the following:
98+
99+
```php
100+
<?php
101+
102+
namespace App\Nova\ThirdPartyIntegrations;
103+
104+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\ThirdParty;
105+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
106+
107+
class SomeThirdPartyField extends ThirdParty implements ThirdPartyContract
108+
{
109+
/**
110+
* Fields array from object.
111+
*
112+
* @return array
113+
*/
114+
public function fields(): array
115+
{
116+
// The following is just an example and should be updated to meet your needs.
117+
return $this->field->customFieldArray;
118+
}
119+
}
120+
```
121+
122+
The name of the class is very important. It should be the same name as the field used within the Nova resource. If the field class is `CustomField`, the third party integration class should also be called `CustomField`.
123+
124+
The `fields()` method should return an array of all the custom field's or package's fields array.
125+
126+
If you feel that the third party integration you've created should be included in this package, please create a pull request and we will look over it!
127+
78128
## Supported Relationships
79129

80130
The following eloquent relationships are currently supported:

config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'third-party' => [
5+
'KirschbaumDevelopment\NovaInlineRelationship\Integrations',
6+
],
7+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Exceptions;
4+
5+
use Exception;
6+
7+
class ContractMissingException extends Exception
8+
{
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts;
4+
5+
interface ThirdPartyContract
6+
{
7+
/**
8+
* ThirdPartyContract constructor.
9+
*
10+
* @param $field
11+
*/
12+
public function __construct($field);
13+
14+
/**
15+
* Fields array from object.
16+
*
17+
* @return array
18+
*/
19+
public function fields(): array;
20+
}

src/Integrations/Field.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations;
4+
5+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
6+
7+
class Field extends ThirdParty implements ThirdPartyContract
8+
{
9+
/**
10+
* Fields array from object.
11+
*
12+
* @return array
13+
*/
14+
public function fields(): array
15+
{
16+
return [$this->field];
17+
}
18+
}

src/Integrations/Integrate.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations;
4+
5+
use ReflectionClass;
6+
use KirschbaumDevelopment\NovaInlineRelationship\Exceptions\ContractMissingException;
7+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Field as FieldInterface;
8+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
9+
10+
class Integrate
11+
{
12+
public static function fields($field): array
13+
{
14+
$basename = class_basename(get_class($field));
15+
16+
foreach (config('nova-inline-relationships.third-party') as $namespace) {
17+
$class = "{$namespace}\\{$basename}";
18+
19+
if (class_exists($class)) {
20+
$reflection = new ReflectionClass($class);
21+
22+
throw_unless(
23+
in_array(ThirdPartyContract::class, $reflection->getInterfaceNames()),
24+
ContractMissingException::class,
25+
sprintf('Third party integration [ %s ] does not implement [ %s ]', $class, ThirdPartyContract::class)
26+
);
27+
28+
return (new $class($field))->fields();
29+
}
30+
}
31+
32+
return (new FieldInterface($field))->fields();
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations;
4+
5+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
6+
7+
class NovaDependencyContainer extends ThirdParty implements ThirdPartyContract
8+
{
9+
/**
10+
* Fields array from object.
11+
*
12+
* @return array
13+
*/
14+
public function fields(): array
15+
{
16+
return $this->field->meta['fields'];
17+
}
18+
}

src/Integrations/Panel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations;
4+
5+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
6+
7+
class Panel extends ThirdParty implements ThirdPartyContract
8+
{
9+
/**
10+
* Fields array from object.
11+
*
12+
* @return array
13+
*/
14+
public function fields(): array
15+
{
16+
return $this->field->data;
17+
}
18+
}

src/Integrations/ThirdParty.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations;
4+
5+
abstract class ThirdParty
6+
{
7+
/**
8+
* @var mixed
9+
*/
10+
protected $field;
11+
12+
/**
13+
* ThirdPartyContract constructor.
14+
*
15+
* @param $field
16+
*/
17+
public function __construct($field)
18+
{
19+
$this->field = $field;
20+
}
21+
}

src/NovaInlineRelationshipServiceProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ class NovaInlineRelationshipServiceProvider extends ServiceProvider
1717
*/
1818
public function boot()
1919
{
20-
Nova::serving(function () {
20+
if ($this->app->runningInConsole()) {
21+
$this->publishes([
22+
__DIR__ . '/../config/config.php' => config_path('nova-inline-relationships.php'),
23+
], 'nova-inline-relationships');
24+
}
25+
26+
Nova::serving(function (ServingNova $event) {
2127
Nova::script('nova-inline-relationship', __DIR__ . '/../dist/js/field.js');
2228
Nova::style('nova-inline-relationship', __DIR__ . '/../dist/css/field.css');
2329
});
@@ -38,5 +44,6 @@ public function boot()
3844
*/
3945
public function register()
4046
{
47+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'nova-inline-relationships');
4148
}
4249
}

0 commit comments

Comments
 (0)