Skip to content

Commit

Permalink
Allow Custom PSR Container Class (#101)
Browse files Browse the repository at this point in the history
Allow custom PSR Container passed in to avoid WordPress errors for 3rd party packages which use psr/container ^1.0.
  • Loading branch information
thefrosty authored Aug 5, 2022
1 parent b7b7a4f commit 2bd6d99
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Plugin/PluginFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TheFrosty\WpUtilities\Plugin;

use Psr\Container\ContainerInterface;

/**
* Class PluginFactory
* @package TheFrosty\WpUtilities\Plugin
Expand Down Expand Up @@ -32,9 +34,10 @@ public static function getInstance(string $slug): Plugin
* @param string $slug Plugin slug.
* @param string|null $filename Optional. Absolute path to the main plugin file.
* This should be passed if the calling file is not the main plugin file.
* @param ContainerInterface|null $container
* @return Plugin A Plugin object instance.
*/
public static function create(string $slug, ?string $filename = ''): Plugin
public static function create(string $slug, ?string $filename = '', ?ContainerInterface $container = null): Plugin
{
if (isset(self::$instances[$slug]) && self::$instances[$slug] instanceof Plugin) {
return self::$instances[$slug];
Expand All @@ -55,7 +58,7 @@ public static function create(string $slug, ?string $filename = ''): Plugin
->setSlug($slug)
->setUrl(\plugin_dir_url($filename));

$plugin = self::setContainer($plugin);
$plugin = self::setContainer($plugin, $container);
$plugin->setTemplateLoader(new TemplateLoader($plugin));
self::$instances[$slug] = $plugin;

Expand All @@ -66,12 +69,13 @@ public static function create(string $slug, ?string $filename = ''): Plugin
* Set the Pimple\Container if it's available.
*
* @param Plugin $plugin
* @param ContainerInterface|null $container
* @return Plugin
*/
private static function setContainer(Plugin $plugin): Plugin
private static function setContainer(Plugin $plugin, ?ContainerInterface $container = null): Plugin
{
if (\class_exists('\Pimple\Container') && \interface_exists('\Psr\Container\ContainerInterface')) {
$plugin->setContainer(new Container());
$plugin->setContainer($container ?? new Container());
}

return $plugin;
Expand Down

0 comments on commit 2bd6d99

Please sign in to comment.