Created
August 14, 2018 15:08
-
-
Save kobus1998/dce8c24434051a1549100d0719a5f4c0 to your computer and use it in GitHub Desktop.
SImple service runner including adapter
This file contains hidden or 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 | |
$serviceContainer = new ServiceContainer(); | |
$serviceContainer->add("my service", MyService::class); | |
$serviceRunner = new ServiceRunner($serviceContainer, MyServiceAdapter::class); | |
$serviceRunner->exec(); |
This file contains hidden or 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 | |
class MyService implements ServiceInterface | |
{ | |
public function boot() { | |
echo "booting..\n"; | |
} | |
public function run() { | |
echo "running...\n"; | |
} | |
} |
This file contains hidden or 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 | |
class MyServiceAdapter implements ServiceRunnerAdapter { | |
public function exec(array $services) { | |
foreach ($services as $service) { | |
$service->run(); | |
} | |
} | |
} |
This file contains hidden or 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 | |
interface ServiceRunnerAdapter { | |
public function exec(array $services); | |
} |
This file contains hidden or 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 | |
class ServiceContainer { | |
private $services; | |
public function add($name, $instance) | |
{ | |
$this->services[$name] = $instance; | |
} | |
public function boot() | |
{ | |
foreach($this->services as $name => $instance) { | |
if (is_string($instance)) { | |
$this->services[$name] = new $instance(); | |
$this->services[$name]->boot(); | |
} | |
} | |
} | |
public function get() | |
{ | |
$this->boot(); | |
return $this->services; | |
} | |
} |
This file contains hidden or 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 | |
interface ServiceInterface { | |
public function run(); | |
} |
This file contains hidden or 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 | |
class ServiceRunner { | |
private $container; | |
private $adapter; | |
public function __construct(ServiceContainer $container, ServiceRunnerAdapter $adapter = null) { | |
$this->container = $container; | |
if ($adapter != null) { | |
if (is_string($adapter)) { | |
$this->adapter = new Adapter(); | |
} else { | |
$this->adapter = $adapter; | |
} | |
} | |
$this->adapter = $adapter; | |
} | |
public function exec() { | |
$services = $this->container->get(); | |
if ($this->adapter != null) { | |
$this->adapter->exec($services); | |
} else { | |
foreach($this->container->get() as $name => $instance) { | |
$instance->run(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment