Skip to content

Commit c5fb85a

Browse files
committed
Ability to add custom third party integrations
Some refactoring and cleaning up PHPCS
1 parent 6701f61 commit c5fb85a

11 files changed

Lines changed: 125 additions & 67 deletions

README.md

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

77+
## Support for Third Party Packages
78+
79+
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.
80+
81+
You must publish the configs for this package with
82+
83+
```shell script
84+
php artisan vendor:publish
85+
```
86+
87+
This will publish a config file as `config/nova-inline-relationships.php`. Add your custom namespaced paths within the `third-party` array. For example:
88+
89+
```php
90+
'third-party' => [
91+
'App\Nova\ThirdPartyIntegrations',
92+
'KirschbaumDevelopment\NovaInlineRelationship\Integrations',
93+
]
94+
```
95+
96+
Create a new class inside that namespace that looks like the following:
97+
98+
```php
99+
<?php
100+
101+
namespace App\Nova\ThirdPartyIntegrations;
102+
103+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\ThirdParty;
104+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
105+
106+
class SomeThirdPartyField extends ThirdParty implements ThirdPartyContract
107+
{
108+
/**
109+
* Fields array from object.
110+
*
111+
* @return array
112+
*/
113+
public function fields(): array
114+
{
115+
// The following is just an example and should be updated to meet your needs.
116+
return $this->field->customFieldArray;
117+
}
118+
}
119+
```
120+
121+
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`.
122+
123+
The `fields()` method should return an array of all the custom field's or package's fields array.
124+
125+
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!
126+
77127
## Supported Relationships
78128

79129
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+
}

src/Integrations/Contracts/ThirdPartyContract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ interface ThirdPartyContract
77
/**
88
* ThirdPartyContract constructor.
99
*
10-
* @param $object
10+
* @param $field
1111
*/
12-
public function __construct($object);
12+
public function __construct($field);
1313

1414
/**
1515
* Fields array from object.

src/Integrations/Field.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@
44

55
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
66

7-
class Field implements ThirdPartyContract
7+
class Field extends ThirdParty implements ThirdPartyContract
88
{
9-
/**
10-
* @var mixed
11-
*/
12-
private $object;
13-
14-
/**
15-
* ThirdPartyContract constructor.
16-
*
17-
* @param $object
18-
*/
19-
public function __construct($object)
20-
{
21-
$this->object = $object;
22-
}
23-
249
/**
2510
* Fields array from object.
2611
*
2712
* @return array
2813
*/
2914
public function fields(): array
3015
{
31-
return [$this->object];
16+
return [$this->field];
3217
}
3318
}

src/Integrations/Integrate.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,33 @@
22

33
namespace KirschbaumDevelopment\NovaInlineRelationship\Integrations;
44

5-
use LeadMarvels\Campaigns\Services\QueryClauses\Contracts\WhereInterface;
5+
use ReflectionClass;
6+
use KirschbaumDevelopment\NovaInlineRelationship\Exceptions\ContractMissingException;
67
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Field as FieldInterface;
8+
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
79

810
class Integrate
911
{
10-
public static function fields($object): array
12+
public static function fields($field): array
1113
{
12-
$basename = class_basename(get_class($object));
13-
$class = "\\KirschbaumDevelopment\\NovaInlineRelationship\\Integrations\\{$basename}";
14+
$basename = class_basename(get_class($field));
1415

15-
return class_exists($class)
16-
? (new $class($object))->fields()
17-
: (new FieldInterface($object))->fields();
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();
1833
}
1934
}

src/Integrations/NovaDependencyContainer.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@
44

55
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
66

7-
class NovaDependencyContainer implements ThirdPartyContract
7+
class NovaDependencyContainer extends ThirdParty implements ThirdPartyContract
88
{
9-
/**
10-
* @var \Laravel\Nova\Panel
11-
*/
12-
private $object;
13-
14-
/**
15-
* ThirdPartyContract constructor.
16-
*
17-
* @param $object
18-
*/
19-
public function __construct($object)
20-
{
21-
$this->object = $object;
22-
}
23-
249
/**
2510
* Fields array from object.
2611
*
2712
* @return array
2813
*/
2914
public function fields(): array
3015
{
31-
return $this->object->meta['fields'];
16+
return $this->field->meta['fields'];
3217
}
3318
}

src/Integrations/Panel.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@
44

55
use KirschbaumDevelopment\NovaInlineRelationship\Integrations\Contracts\ThirdPartyContract;
66

7-
class Panel implements ThirdPartyContract
7+
class Panel extends ThirdParty implements ThirdPartyContract
88
{
9-
/**
10-
* @var \Laravel\Nova\Panel
11-
*/
12-
private $object;
13-
14-
/**
15-
* ThirdPartyContract constructor.
16-
*
17-
* @param $object
18-
*/
19-
public function __construct($object)
20-
{
21-
$this->object = $object;
22-
}
23-
249
/**
2510
* Fields array from object.
2611
*
2712
* @return array
2813
*/
2914
public function fields(): array
3015
{
31-
return $this->object->data;
16+
return $this->field->data;
3217
}
3318
}

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class NovaInlineRelationshipServiceProvider extends ServiceProvider
1818
*/
1919
public function boot()
2020
{
21+
if ($this->app->runningInConsole()) {
22+
$this->publishes([
23+
__DIR__ . '/../config/config.php' => config_path('nova-inline-relationships.php'),
24+
], 'nova-inline-relationships');
25+
}
26+
2127
Nova::serving(function (ServingNova $event) {
2228
Nova::script('nova-inline-relationship', __DIR__ . '/../dist/js/field.js');
2329
Nova::style('nova-inline-relationship', __DIR__ . '/../dist/css/field.css');
@@ -39,5 +45,6 @@ public function boot()
3945
*/
4046
public function register()
4147
{
48+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'nova-inline-relationships');
4249
}
4350
}

0 commit comments

Comments
 (0)