php-shell/lib/Context.php

86 lines
1.7 KiB
PHP

<?php
namespace NoccyLabs\Shell;
class Context
{
protected $name;
protected $commands = [];
protected $data = [];
public function __construct($name=null, array $data=[])
{
$this->name = $name;
$this->data = $data;
$this->configure();
}
protected function configure()
{
// Override this to do setup stuff
}
public function addCommand($command, callable $handler)
{
$this->commands[$command] = $handler;
}
public function addCommands(array $commands)
{
foreach ($commands as $command=>$handler) {
// Make it easier to connect commands direct to local functions
if (is_string($handler) && is_callable([$this,$handler])) {
$handler = [ $this,$handler ];
}
// Add the command to the command list
$this->addCommand($handler);
}
}
public function hasCommand($command)
{
return array_key_exists($command, $this->commands);
}
public function getCommand($command)
{
return $this->commands[$command];
}
public function getName()
{
return $this->name;
}
public function __get($key)
{
if (!array_key_exists($key,$this->data)) {
return false;
}
return $this->data[$key];
}
public function __set($key,$value)
{
$this->data[$key] = $value;
}
public function __isset($key)
{
return array_key_exists($key);
}
public function __unset($key)
{
unset($this->data[$key]);
}
public function getData()
{
return $this->data;
}
}