Skip to content

Commit

Permalink
Merge pull request #1 from GravityKit/prepare-league-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg authored Sep 25, 2023
2 parents 2e816a1 + c268e01 commit 6d476e6
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/Addon/AddonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public static function set_instance(AddonInterface $addon): void;
/**
* Retrieve the current instance.
* @since $ver$
* @return AddonInterface The current instance of this add on.
* @return static The current instance of this add-on.
*/
public static function get_instance(): AddonInterface;

/**
* Sets the assets directory for the current plugin.
* @since $ver$
* @param string $assets_dir The assets directory.
* @param string $assets_dir The assets' directory.
*/
public function setAssetsDir(string $assets_dir): void;
}
}
1 change: 1 addition & 0 deletions src/Addon/AddonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static function set_instance(AddonInterface $addon): void
/**
* @inheritdoc
* @since $ver$
* @return static
*/
public static function get_instance(): AddonInterface
{
Expand Down
63 changes: 63 additions & 0 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace GFExcel\Container;

use League\Container\Container as LeagueContainer;
use League\Container\ReflectionContainer;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Container implementation backed by the League Container package.
* @since $ver$
*/
final class Container implements ContainerInterface
{
/**
* The league container class.
* @since $ver$
* @var LeagueContainer
*/
private $container;

public function __construct()
{
$this->container = new LeagueContainer();
$this->container
->defaultToShared()
->delegate(new ReflectionContainer());
}

/**
* @inheritDoc
* @since $ver$
*/
public function addServiceProvider(ServiceProviderInterface $provider) : ContainerInterface
{
$this->container->addServiceProvider($provider);

return $this;
}

/**
* @inheritDoc
* @since $ver$
*/
public function get(string $id)
{
try {
return $this->container->get($id);
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
return null;
}
}

/**
* @inheritDoc
* @since $ver$
*/
public function has(string $id) : bool
{
return $this->container->has($id);
}
}
7 changes: 2 additions & 5 deletions src/Container/ContainerAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace GFExcel\Container;

use League\Container\Container;
use Psr\Container\ContainerInterface;

/**
* Trait that makes a class contiainer aware.
* Trait that makes a class container aware.
* @since $ver$
*/
trait ContainerAware
Expand All @@ -31,7 +28,7 @@ public function setContainer(ContainerInterface $container)
/**
* Get the container instance for this class.
* @since $ver$
* @return Container|null The container instance.
* @return ContainerInterface|null The container instance.
*/
public function getContainer(): ?ContainerInterface
{
Expand Down
33 changes: 33 additions & 0 deletions src/Container/ContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace GFExcel\Container;

/**
* The container contract.
* @since $ver$
*/
interface ContainerInterface
{
/**
* Registers a service provider which registers services with the container.
* @since $ver$
* @return static
*/
public function addServiceProvider(ServiceProviderInterface $provider) : self;

/**
* Returns the service by the service ID.
* @since $ver$
* @param string $id The service ID.
* @return mixed|null The service or `null`.
*/
public function get(string $id);

/**
* Whether the container holds the provided service ID.
* @since $ver$
* @param string $id The service ID.
* @return bool
*/
public function has(string $id) : bool;
}
10 changes: 10 additions & 0 deletions src/Container/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GFExcel\Container;

use League\Container\ServiceProvider\BootableServiceProviderInterface;
use League\Container\ServiceProvider\ServiceProviderInterface as LeagueServiceProviderInterface;

interface ServiceProviderInterface extends LeagueServiceProviderInterface, BootableServiceProviderInterface
{
}
8 changes: 4 additions & 4 deletions src/Plugin/BasePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace GFExcel\Plugin;

use GFExcel\Addon\AddonInterface;
use League\Container\Container;
use GFExcel\Container\ContainerInterface;

/**
* A base for a plugin to extend from.
Expand All @@ -21,7 +21,7 @@ abstract class BasePlugin
/**
* The container instance.
* @since $ver$
* @var Container
* @var ContainerInterface
*/
protected $container;

Expand All @@ -34,10 +34,10 @@ abstract class BasePlugin

/**
* Creates the plugin.
* @param Container $container The service container.
* @param ContainerInterface $container The service container.
* @param string|null $assets_dir The assets directory.
*/
public function __construct(Container $container, string $assets_dir = null)
public function __construct(ContainerInterface $container, string $assets_dir = null)
{
$this->container = $container;
$this->assets_dir = $assets_dir;
Expand Down
52 changes: 47 additions & 5 deletions src/ServiceProvider/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
namespace GFExcel\ServiceProvider;

use GFExcel\Action\ActionAwareInterface;
use GFExcel\Container\ServiceProviderInterface;
use League\Container\Container;
use League\Container\Definition\DefinitionInterface;
use League\Container\ServiceProvider\AbstractServiceProvider as LeagueAbstractServiceProviderAlias;

/**
* Abstract service provider that provides helper methods.
* @since $ver$
*/
abstract class AbstractServiceProvider extends LeagueAbstractServiceProviderAlias
abstract class AbstractServiceProvider extends LeagueAbstractServiceProviderAlias implements
ServiceProviderInterface
{
/**
* List of classes the service provider provides.
* @since $ver$
*/
protected $provides = [];

/**
* Helper method to quickly add an action.
* @since $ver$
Expand All @@ -20,10 +29,43 @@ abstract class AbstractServiceProvider extends LeagueAbstractServiceProviderAlia
* @param bool|null $shared Whether this is a shared instance.
* @return DefinitionInterface The definition.
*/
protected function addAction(string $id, $concrete = null, ?bool $shared = null): DefinitionInterface
protected function addAction(string $id, $concrete = null, ?bool $shared = null) : DefinitionInterface
{
$container = $this->getContainer();
$definition = $shared
? $container->addShared($id, $concrete)
: $container->add($id, $concrete);

return $definition->addTag(ActionAwareInterface::ACTION_TAG);
}

/**
* Whether this service provide provides the requested service id.
* @since $ver$
*/
public function provides(string $id) : bool
{
return in_array($id, $this->provides, true);
}

/**
* Backwards compatability for plugins.
* @since $ver$
* @return Container
* @deprecated Use getContainer instead.
*/
public function getLeagueContainer() : Container
{
return $this->getContainer();
}

/**
* Method will be invoked on registration of a service provider implementing
* this interface. Provides ability for eager loading of Service Providers.
*
* @return void
*/
public function boot() : void
{
return $this->getLeagueContainer()
->add($id, $concrete, $shared)
->addTag(ActionAwareInterface::ACTION_TAG);
}
}
9 changes: 4 additions & 5 deletions src/ServiceProvider/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
use GFExcel\Template\TemplateAwareInterface;
use GFExcel\Repository\FormRepository;
use GFExcel\Repository\FormRepositoryInterface;
use League\Container\ServiceProvider\BootableServiceProviderInterface;

/**
* The service provider for the base of GFExcel.
* @since $ver$
*/
class BaseServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
class BaseServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
Expand All @@ -32,7 +31,7 @@ class BaseServiceProvider extends AbstractServiceProvider implements BootableSer
*/
public function register(): void
{
$container = $this->getLeagueContainer();
$container = $this->getContainer();

$container->add(
FormRepositoryInterface::class,
Expand All @@ -49,7 +48,7 @@ public function register(): void
*/
protected function getActions(): array
{
$container = $this->getLeagueContainer();
$container = $this->getContainer();
if (!$container->has(ActionAwareInterface::ACTION_TAG)) {
return [];
}
Expand All @@ -63,7 +62,7 @@ protected function getActions(): array
*/
public function boot(): void
{
$container = $this->getLeagueContainer();
$container = $this->getContainer();

$container
->inflector(ActionAwareInterface::class, function (ActionAwareInterface $instance) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Container/ContainerAwareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace GFExcel\Tests\Container;

use GFExcel\Container\ContainerAware;
use GFExcel\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

/**
* Unit tests for {@see ContainerAware}.
Expand Down

0 comments on commit 6d476e6

Please sign in to comment.