Maslosoft Signals API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/signals
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/signals/
*/
namespace Maslosoft\Signals;
use Maslosoft\Addendum\Utilities\ClassChecker;
use Maslosoft\Addendum\Utilities\NameNormalizer;
use Maslosoft\Cli\Shared\ConfigReader;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\Signals\Builder\Addendum;
use Maslosoft\Signals\Builder\IO\PhpFile;
use Maslosoft\Signals\Factories\FilterFactory;
use Maslosoft\Signals\Factories\SlotFactory;
use Maslosoft\Signals\Helpers\PostFilter;
use Maslosoft\Signals\Helpers\PreFilter;
use Maslosoft\Signals\Interfaces\BuilderIOInterface;
use Maslosoft\Signals\Interfaces\ExtractorInterface;
use Maslosoft\Signals\Interfaces\FilterInterface;
use Maslosoft\Signals\Interfaces\PostFilterInterface;
use Maslosoft\Signals\Interfaces\PreFilterInterface;
use Maslosoft\Signals\Interfaces\SignalAwareInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use ReflectionClass;
use UnexpectedValueException;
/**
* Main signals components
*
* @author Piotr
* @property LoggerInterface $logger Logger, set this to log warnings, notices errors. This is shorthand for `get/setLogger`.
*/
class Signal implements LoggerAwareInterface
{
const Slots = 'slots';
const Signals = 'signals';
/**
* Generated signals name.
* Name of this constant is confusing.
* @internal description
*/
const ConfigFilename = 'signals-definition.php';
/**
* Config file name
*/
const ConfigName = "signals";
/**
* Runtime path is directory where config cache from yml file will
* be stored. Path is relative to project root. This must be writable
* by command line user.
*
* @var string
*/
public $runtimePath = 'runtime';
/**
* This paths will be searched for `SlotFor` and `SignalFor` annotations.
*
*
*
* TODO Autodetect based on composer autoload
*
* @var string[]
*/
public $paths = [
'vendor',
];
/**
* Directories to ignore while scanning
* @var string[]
*/
public $ignoreDirs = [
'vendor', // Vendors in vendors
'generated', // Generated data, including signals
'runtime', // Runtime data
];
/**
* Filters configuration.
* This filters will be applied to every emit. This property
* should contain array of class names implementing filters.
* @var string[]|object[]
*/
public $filters = [];
/**
* Sorters configuration.
* @var string[]|object[]
*/
public $sorters = [];
/**
* Extractor configuration
* @var string|[]|object
*/
public $extractor = Addendum::class;
/**
* Input/Output configuration, at minimum it should
* contain class name for builder input output interface.
* It can also contain array [configurable options for IO class](php-io/).
*
*
*
* @var string|[]|object
*/
public $io = PhpFile::class;
/**
* Whenever component is initialized
* @var bool
*/
private $isInitialized = false;
/**
* Configuration of signals and slots
* @var string[][]
*/
private static $config = [];
/**
* Logger instance holder
* NOTE: There is property annotation with `logger` name,
* thus this name is a bit longer
* @var LoggerInterface
*/
private $loggerInstance = null;
/**
* Embedded dependency injection
* @var EmbeDi
*/
private $di = null;
/**
* Version
* @var string
*/
private $version = null;
/**
* Current filters
* @var PreFilterInterface[]|PostFilterInterface[]
*/
private $currentFilters = [];
public function __construct($configName = self::ConfigName)
{
$this->loggerInstance = new NullLogger;
/**
* TODO This should be made as embedi adapter, currently unsupported
*/
$config = new ConfigReader($configName);
$this->di = EmbeDi::fly($configName);
$this->di->configure($this);
$this->di->apply($config->toArray(), $this);
}
/**
* Getter
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->{'get' . ucfirst($name)}();
}
/**
* Setter
* @param string $name
* @param mixed $value
* @return mixed
*/
public function __set($name, $value)
{
return $this->{'set' . ucfirst($name)}($value);
}
/**
* Get current signals version
*
* @codeCoverageIgnore
* @return string
*/
public function getVersion()
{
if (null === $this->version)
{
$this->version = require __DIR__ . '/version.php';
}
return $this->version;
}
public function init()
{
if (!$this->isInitialized)
{
$this->reload();
}
if (!$this->di->isStored($this))
{
$this->di->store($this);
}
}
/**
* Apply filter to current emit.
*
* Pass false as param to disable all filters.
*
* @param FilterInterface|string|mixed $filter
* @return Signal
* @throws UnexpectedValueException
*/
public function filter($filter)
{
// disable filters
if (is_bool($filter) && false === $filter)
{
$this->currentFilters = [];
return $this;
}
// Instantiate from string or array
if (!is_object($filter))
{
$filter = $this->di->apply($filter);
}
if (!$filter instanceof PreFilterInterface && !$filter instanceof PostFilterInterface)
{
throw new UnexpectedValueException(sprintf('$filter must implement either `%s` or `%s` interface', PreFilterInterface::class, PostFilterInterface::class));
}
$this->currentFilters[] = $filter;
return $this;
}
/**
* Emit signal to inform slots
* @param object|string $signal
* @return object[]
*/
public function emit($signal)
{
$result = [];
if (is_string($signal))
{
$signal = new $signal;
}
$name = get_class($signal);
NameNormalizer::normalize($name);
if (empty(self::$config))
{
$this->init();
}
if (!isset(self::$config[self::Signals][$name]))
{
self::$config[self::Signals][$name] = [];
$this->loggerInstance->debug('No slots found for signal `{name}`, skipping', ['name' => $name]);
return $result;
}
foreach (self::$config[self::Signals][$name] as $fqn => $injections)
{
// Skip
if (false === $injections || count($injections) == 0)
{
continue;
}
if (!PreFilter::filter($this, $fqn, $signal))
{
continue;
}
foreach ($injections as $injection)
{
$injected = SlotFactory::create($this, $signal, $fqn, $injection);
if (false === $injected)
{
continue;
}
if (!PostFilter::filter($this, $injected, $signal))
{
continue;
}
$result[] = $injected;
}
}
$this->currentFilters = [];
return $result;
}
/**
* Call for signals from slot
* @param object $slot
* @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get into slot
*/
public function gather($slot, $interface = null)
{
$name = get_class($slot);
NameNormalizer::normalize($name);
if (!empty($interface))
{
NameNormalizer::normalize($interface);
}
if (empty(self::$config))
{
$this->init();
}
if (!isset(self::$config[self::Slots][$name]))
{
self::$config[self::Slots][$name] = [];
$this->loggerInstance->debug('No signals found for slot `{name}`, skipping', ['name' => $name]);
}
$result = [];
foreach ((array) self::$config[self::Slots][$name] as $fqn => $emit)
{
if (false === $emit)
{
continue;
}
if (!PreFilter::filter($this, $fqn, $slot))
{
continue;
}
// Check if class exists and log if doesn't
if (!ClassChecker::exists($fqn))
{
$this->loggerInstance->debug(sprintf("Class `%s` not found while gathering slot `%s`", $fqn, get_class($slot)));
continue;
}
if (null === $interface)
{
$injected = new $fqn;
if (!PostFilter::filter($this, $injected, $slot))
{
continue;
}
$result[] = $injected;
continue;
}
// Check if it's same as interface
if ($fqn === $interface)
{
$injected = new $fqn;
if (!PostFilter::filter($this, $injected, $slot))
{
continue;
}
$result[] = $injected;
continue;
}
$info = new ReflectionClass($fqn);
// Check if class is instance of base class
if ($info->isSubclassOf($interface))
{
$injected = new $fqn;
if (!PostFilter::filter($this, $injected, $slot))
{
continue;
}
$result[] = $injected;
continue;
}
$interfaceInfo = new ReflectionClass($interface);
// Check if class implements interface
if ($interfaceInfo->isInterface() && $info->implementsInterface($interface))
{
$injected = new $fqn;
if (!PostFilter::filter($this, $injected, $slot))
{
continue;
}
$result[] = $injected;
continue;
}
}
return $result;
}
/**
* Get filters
* @param string $interface
* @return PreFilterInterface[]|PostFilterInterface[]
*/
public function getFilters($interface)
{
$filters = FilterFactory::create($this, $interface);
foreach ($this->currentFilters as $filter)
{
if (!$filter instanceof $interface)
{
continue;
}
$filters[] = $filter;
}
return $filters;
}
/**
* Get logger
* @codeCoverageIgnore
* @return LoggerInterface
*/
public function getLogger()
{
return $this->loggerInstance;
}
/**
* Set logger
* @codeCoverageIgnore
* @param LoggerInterface $logger
* @return Signal
*/
public function setLogger(LoggerInterface $logger)
{
$this->loggerInstance = $logger;
return $this;
}
/**
* Get dependency injection container
* @return EmbeDi
*/
public function getDi()
{
return $this->di;
}
public function setDi(EmbeDi $di)
{
$this->di = $di;
return $this;
}
/**
* Get Input/Output adapter
* @codeCoverageIgnore
* @return BuilderIOInterface I/O Adapter
*/
public function getIO()
{
return $this->getConfigured('io');
}
/**
* Set Input/Output interface
* @codeCoverageIgnore
* @param BuilderIOInterface $io
* @return Signal
*/
public function setIO(BuilderIOInterface $io)
{
return $this->setConfigured($io, 'io');
}
/**
* @codeCoverageIgnore
* @return ExtractorInterface
*/
public function getExtractor()
{
return $this->getConfigured('extractor');
}
/**
* @codeCoverageIgnore
* @param ExtractorInterface $extractor
* @return Signal
*/
public function setExtractor(ExtractorInterface $extractor)
{
return $this->setConfigured($extractor, 'extractor');
}
/**
* Reloads signals cache and reinitializes component.
*/
public function resetCache()
{
$this->reload();
}
private function reload()
{
self::$config = $this->getIO()->read();
}
/**
* Get configured property
* @param string $property
* @return SignalAwareInterface
*/
private function getConfigured($property)
{
if (is_object($this->$property))
{
$object = $this->$property;
}
else
{
$object = $this->di->apply($this->$property);
}
if ($object instanceof SignalAwareInterface)
{
$object->setSignal($this);
}
return $object;
}
/**
* Set signal aware property
* @param SignalAwareInterface $object
* @param string $property
* @return Signal
*/
private function setConfigured(SignalAwareInterface $object, $property)
{
$object->setSignal($this);
$this->$property = $object;
return $this;
}
}
API documentation generated by ApiGen