/** * Instantiate a Factory object. * * @since 1.0.0 * * @param ConfigInterface $config Configuration settings. * @param string $element The type of element to instantiate a factory for. * * @throws FailedToInstantiateFactory When an unknown element type is requested. */ public function __construct(ConfigInterface $config, $element) { $this->config = $config; if (!$this->config->hasKey($element)) { throw new FailedToInstantiateFactory(sprintf('Could not instantiate Factory for unknown Element Type "%1$s".', $element)); } $this->element = $element; }
/** * Register mapping definitions. * * Takes a ConfigInterface and reads the following keys to add definitions: * - 'sharedAliases' * - 'standardAliases' * - 'argumentDefinitions' * - 'argumentProviders' * - 'delegations' * - 'preparations' * * @since 0.1.0 * * @param ConfigInterface $config Config file to parse. * * @throws InvalidMappingsException If a needed key could not be read from the config file. * @throws InvalidMappingsException If the dependency injector could not be set up. */ public function registerMappings(ConfigInterface $config) { $configKeys = [static::K_STANDARD_ALIASES => 'mapAliases', static::K_SHARED_ALIASES => 'shareAliases', static::K_ARGUMENT_DEFINITIONS => 'defineArguments', static::K_ARGUMENT_PROVIDERS => 'defineArgumentProviders', static::K_DELEGATIONS => 'defineDelegations', static::K_PREPARATIONS => 'definePreparations']; try { foreach ($configKeys as $key => $method) { ${$key} = $config->hasKey($key) ? $config->getKey($key) : []; } $standardAliases = array_merge($sharedAliases, $standardAliases); } catch (Exception $exception) { throw new InvalidMappingsException(sprintf(_('Failed to read needed keys from config. Reason: "%1$s".'), $exception->getMessage())); } try { foreach ($configKeys as $key => $method) { array_walk(${$key}, [$this, $method]); } } catch (Exception $exception) { throw new InvalidMappingsException(sprintf(_('Failed to set up dependency injector. Reason: "%1$s".'), $exception->getMessage())); } }
/** * Check whether the Config has a specific key. * * To get a value several levels deep, add the keys for each level as a comma-separated list. * * @since 0.1.2 * @since 0.1.5 Accepts list of keys. * * @param string|array $_ List of keys. * * @return bool Whether the key is known. */ protected function hasConfigKey($_) { $keys = func_get_args(); return $this->config->hasKey($keys); }