Last active
December 17, 2015 21:49
-
-
Save EclipseGc/5677953 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains \Drupal\Core\Plugin\ContainerFactoryPluginBase. | |
*/ | |
namespace Drupal\Core\Plugin; | |
use Drupal\Core\Plugin\ContextAwarePluginBase; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Defines a base plugin that can pull it's dependencies from the container. | |
*/ | |
class ContainerFactoryPluginBase extends ContextAwarePluginBase { | |
/** | |
* Creates an instance of the plugin. | |
* | |
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container | |
* The container to pull out services used in the plugin. | |
* @param array $configuration | |
* A configuration array containing information about the plugin instance. | |
* @param string $plugin_id | |
* The plugin_id for the plugin instance. | |
* @param array $plugin_definition | |
* The plugin implementation definition. | |
* | |
* @return static | |
* Returns an instance of this plugin. | |
*/ | |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { | |
$plugin = new static($configuration, $plugin_id, $plugin_definition); | |
$plugin->processServices($container, $configuartion, $plugin_id, $plugin_definition); | |
return $plugin; | |
} | |
public function processServices(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
if (isset($plugin_definition['services']) { | |
foreach ($plugin_definition['services'] as $setter => $service_id) { | |
if (empty($configuration['services'][$service_id]) && $container->has($service_id)) { | |
$plugin->$setter($container->get($service_id)); | |
} | |
elseif (!empty($configuration['services'][$service_id])) { | |
$plugin->$setter($configuration['services'][$service_id]); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment