Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Custom PSR Container Class #101

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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