Skip to content

Instantly share code, notes, and snippets.

@lpj145
Created December 12, 2017 01:27
Show Gist options
  • Save lpj145/ef36c6bc2972fcd2875b7e1b3a9f2659 to your computer and use it in GitHub Desktop.
Save lpj145/ef36c6bc2972fcd2875b7e1b3a9f2659 to your computer and use it in GitHub Desktop.
<?php
namespace ExpressiveProvider;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
abstract class BaseProvider
{
/**
* @var array
*/
private $providerArray = [];
public function __invoke()
{
$this->register();
return $this->providerArray;
}
/**
* Register all providers
*/
abstract protected function register() : void;
/**
* @param string $contract
* @param null $service
* @return mixed
*/
protected function invokables(string $contract, $service)
{
return $this->addToDependencies('invokables', $contract, $service);
}
/**
* @param string $contract
* @param null $service
* @return mixed
*/
protected function factory(string $contract, $service)
{
return $this->addToDependencies('factories', $contract, $service);
}
/**
* @param string $name
* @param null $service
* @return mixed
*/
protected function aliases(string $name, $service)
{
return $this->addToDependencies('aliases', $name, $service);
}
/**
* @param string $name
* @param null $configs
* @return \Closure|null
*/
protected function config(string $name, $configs)
{
$this->providerArray[$name] = $configs;
}
/**
* @param string $contract
* @param $service
*/
protected function abstractService(string $contract, $service)
{
if (is_string($service)) {
$service = [$service];
}
$this->providerArray[ConfigAbstractFactory::class][$contract] = $service;
}
/**
* @param string $namespace
* @param string $serviceName
* @param null $service
* @return \Closure|null
*/
private function addToDependencies(string $namespace, string $serviceName, $service)
{
$this->providerArray['dependencies'][$namespace][$serviceName] = $service;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment