Page MenuHomePhabricator

No OneTemporary

diff --git a/core/event/Listener.php b/core/event/Publisher.php
similarity index 89%
rename from core/event/Listener.php
rename to core/event/Publisher.php
index 6d845cb..1f64c7b 100644
--- a/core/event/Listener.php
+++ b/core/event/Publisher.php
@@ -1,84 +1,84 @@
<?php namespace spitfire\core\event;
use Closure;
/*
* The MIT License
*
* Copyright 2019 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* The listener represents the leaf of a target tree, but generates a binary Listener
* tree below it.
*
* @author César de la Cal Bretschneider <cesar@magic3w.com>
*/
-class Listener extends Pluggable
+class Publisher extends Tree
{
/**
*
*
* @var Closure[]
*/
private $body = [];
/**
*
* @param Closure $callable
* @return $this
*/
- public function do(Closure$callable) {
+ public function subscribe(Closure$callable) {
$this->body[] = $callable;
return $this;
}
public function remove(Closure$callable) {
unset($this->body[array_search($callable, $this->body, true)]);
return true;
}
public function _body($parameter) {
$arg = $parameter;
foreach ($this->body as $callable) {
$arg = $callable($arg)?: $arg;
}
return $arg;
}
/**
* Run is the main function of the pluggable object, when invoked, it will first
* execute all the registered before objects, then the body, and finally, the
* after code.
*
* @param mixed $parameter
*/
- public function run($parameter) {
- $parameter = $this->before? $this->before->run($parameter) : $parameter;
+ public function update($parameter) {
+ $parameter = $this->before? $this->before->update($parameter) : $parameter;
$parameter = $this->_body($parameter)?: $parameter;
- return $this->after? $this->after->run($parameter) : $parameter;
+ return $this->after? $this->after->update($parameter) : $parameter;
}
}
diff --git a/core/event/Target.php b/core/event/PublisherTree.php
similarity index 92%
rename from core/event/Target.php
rename to core/event/PublisherTree.php
index 74f09f0..94e1c3d 100644
--- a/core/event/Target.php
+++ b/core/event/PublisherTree.php
@@ -1,105 +1,105 @@
<?php namespace spitfire\core\event;
/*
* The MIT License
*
* Copyright 2019 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* A plugin target provides a namespace in which listeners and triggers can
* connect.
*
* NOTE: Plugins are a costly component of every application and should
* therefore be provided carefully and deliberately to create very loose coupling
* of the application and the plugin.
*
* @author César de la Cal Bretschneider <cesar@magic3w.com>
*/
-class Target extends Pluggable
+class PublisherTree extends Tree
{
/**
* The nested targets within this instance.
*
- * @var Target[]
+ * @var PublisherTree[]
*/
private $children = [];
/**
* This is a convenience method, for providing the ability to users to quickly
* create complicated and nested listeners for events.
*
* For example the following code:
* <code>
* $plugins = new Target();
* $plugins->comments->form->before()->do(function ($e) { return something($e); });
* </code>
*
* Makes it easy to understand what the user is attempting to do. Events do
* not bubble though, the feature just makes it easier to understand.
*
* @param string $name
- * @return Target
+ * @return PublisherTree
*/
public function __get($name) {
if (isset($this->children[$name])) {
return $this->children[$name];
}
else {
- return $this->children[$name] = new Target();
+ return $this->children[$name] = new PublisherTree();
}
}
/**
* Triggers an event. The object is then passed along to every listener and
* the result of the listener overrides the object it was provided (in case
* the listener does return anything).
*
* This code:
* <pre><code>
* $obj = new Object();
* $plugins->form->do(function ($o) { return something($o); }, $obj);
* </code></pre>
*
* Is functionally identical to:
* <pre><code>
* $obj = new Object();
* $obj = $plugins->form->before()->run($obj);
* $obj = something($obj);
* $obj = $plugins->form->after()->run($obj);
* </code></pre>
*
* While the second sample seems way more convoluted (and it is), it may be a
* good option for implementing plugins that should not end in a tree of nested
* functions calling the events of their components.
*
* @param Closure|callable $operation
* @param mixed $object
* @return mixed
*/
public function do($operation, $object) {
- $before = $this->before()->run($object);
- return $this->after()->run($operation($before)?: $before);
+ $before = $this->before()->update($object);
+ return $this->after()->update($operation($before)?: $before);
}
}
diff --git a/core/event/Pluggable.php b/core/event/RecipeLoader.php
similarity index 53%
copy from core/event/Pluggable.php
copy to core/event/RecipeLoader.php
index 33e3973..c0c02f5 100644
--- a/core/event/Pluggable.php
+++ b/core/event/RecipeLoader.php
@@ -1,57 +1,67 @@
<?php namespace spitfire\core\event;
/*
* The MIT License
*
* Copyright 2019 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
-/**
- * Pluggable is the base class to both listeners and targets, since both have
- * dependencies and dependents that need to be executed.
- *
- * Dependencies and dependents are called before and after respectively to make
- * it easier to understand which code is executed first.
- */
-abstract class Pluggable
+class RecipeLoader
{
- protected $before;
-
- protected $after;
+ private static $loaded = false;
- public function before() {
- if (!$this->before) {
- $this->before = new Listener();
- }
+ /**
+ * Loads all the recipes that the system needs to ensure that the events get
+ * dispatched. Events get only loaded whenever the event() function is actually
+ * called, which should make the footprint of the event system negligible when
+ * it is not being used.
+ *
+ * Core components must not invoke the event() function or call this method.
+ *
+ * @todo This should implement a caching mechanism so the directory does not need to
+ * be scanned with every request that uses it
+ * @return void
+ */
+ public static function import() {
+ /*
+ * Obviously, if the listeners have been appropriately loaded, we can stop
+ * them from being imported again.
+ */
+ if (self::$loaded) { return; }
- return $this->before;
- }
-
- public function after() {
- if (!$this->after) {
- $this->after = new Listener();
+ /*
+ * Find all the PHP files in the recipes folder. These are loaded to allow
+ * them to register with the application.
+ */
+ $recipes = glob(basedir() . '/bin/recipes/*.php');
+
+ /*
+ * Loop over the recipes, and include them all.
+ */
+ foreach ($recipes as $recipe) {
+ include $recipe;
}
- return $this->after;
+ self::$loaded = true;
}
}
diff --git a/core/event/Pluggable.php b/core/event/Tree.php
similarity index 94%
rename from core/event/Pluggable.php
rename to core/event/Tree.php
index 33e3973..2e5173e 100644
--- a/core/event/Pluggable.php
+++ b/core/event/Tree.php
@@ -1,57 +1,57 @@
<?php namespace spitfire\core\event;
/*
* The MIT License
*
* Copyright 2019 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Pluggable is the base class to both listeners and targets, since both have
* dependencies and dependents that need to be executed.
*
* Dependencies and dependents are called before and after respectively to make
* it easier to understand which code is executed first.
*/
-abstract class Pluggable
+abstract class Tree
{
protected $before;
protected $after;
public function before() {
if (!$this->before) {
- $this->before = new Listener();
+ $this->before = new Publisher();
}
return $this->before;
}
public function after() {
if (!$this->after) {
- $this->after = new Listener();
+ $this->after = new Publisher();
}
return $this->after;
}
}
diff --git a/core/functions.php b/core/functions.php
index 1495498..7635818 100644
--- a/core/functions.php
+++ b/core/functions.php
@@ -1,353 +1,382 @@
<?php
use spitfire\App;
use spitfire\core\Collection;
use spitfire\core\ContextInterface;
use spitfire\core\Environment;
+use spitfire\core\event\PublisherTree;
use spitfire\core\http\URL;
+use spitfire\exceptions\ExceptionHandler;
+use spitfire\exceptions\ExceptionHandlerCLI;
use spitfire\io\cli\Console;
+use spitfire\io\curl\Request;
+use spitfire\io\media\FFMPEGManipulator;
+use spitfire\io\media\GDManipulator;
+use spitfire\io\media\ImagickManipulator;
+use spitfire\io\media\MediaDispatcher;
use spitfire\locale\Domain;
use spitfire\locale\DomainGroup;
use spitfire\locale\Locale;
use spitfire\SpitFire;
use spitfire\SpitFireCLI;
-use spitfire\storage\database\DB;
use spitfire\storage\database\Settings;
+use spitfire\storage\drive\MountPoint;
+use spitfire\storage\objectStorage\DriveDispatcher;
+use spitfire\storage\objectStorage\NodeInterface;
use spitfire\validation\ValidationException;
use spitfire\validation\Validator;
use spitfire\validation\ValidatorInterface;
/**
* This is a quick hand method to use Spitfire's main App class as a singleton.
* It allows you to quickly access many of the components the framework provides
* to make it easier to read and maintain the code being created.
*
* @staticvar type $sf
- * @return \spitfire\SpitFire
+ * @return SpitFire
*/
function spitfire() {
static $sf = null;
if ($sf !== null) {
return $sf;
} else {
$sf = php_sapi_name() === 'cli'? new SpitFireCLI() : new SpitFire();
$sf->enable();
return $sf;
}
}
/**
*
* Registers a new Application in Spitfire, allowing it to handle requests directed
* to it.
*
* @param string $name The name of the Application
* @param string $namespace The namespace in which the requests will be sent to
* the application.
* @return App The App created by the system, use this to pass parameters and
* configuration to the application.
*/
function app($name, $namespace) {
$appName = $name . 'App';
$app = new $appName(APP_DIRECTORY . $name . DIRECTORY_SEPARATOR, $namespace);
spitfire()->registerApp($app, $namespace);
return $app;
}
/**
* Shorthand function to create / retrieve the model the application is using
* to store data. We could consider this a little DB handler factory.
*
* @param Settings $options
* @return \spitfire\storage\database\DB
*/
function db(Settings$options = null) {
static $db = null;
#If we're requesting the standard driver and have it cached, we use this
if ($options === null && $db !== null) { return $db; }
#If no options were passed, we try to fetch them from the environment
$settings = Settings::fromURL($options? : Environment::get('db'));
#Instantiate the driver
$driver = 'spitfire\storage\database\drivers\\' . $settings->getDriver() . '\Driver';
$driver = new $driver($settings);
#If no options were provided we will assume that this is the standard DB handler
if ($options === null) { $db = $driver; }
#Return the driver
return $driver;
}
/**
* Returns HTML escaped string and if desired it adds ellipsis. If the string is
* numeric it will reduce unnecessary decimals.
*
* @param String $str
* @param int $maxlength
* @return String
*/
function __($str, $maxlength = false) {
if ($maxlength) { $str = Strings::ellipsis ($str, $maxlength); }
if (defined('ENT_HTML5'))
{ $str = htmlspecialchars($str, ENT_HTML5, Environment::get('system_encoding')); }
else
{ $str = htmlspecialchars($str, ENT_COMPAT, Environment::get('system_encoding')); }
return $str;
}
/**
* Translation helper.
*
* Depending on the arguments this function receives, it will have one of several
* behaviors.
*
* If the first argument is a spitfire\locale\Locale and the function receives a
* optional second parameter, then it will assign the locale to either the global
* domain / the domain provided in the second parameter.
*
* Otherwise, if the first parameter is a string, it will call the default locale's
* say method. Which will translate the string using the standard locale.
*
* If no parameters are provided, this function returns a DomainGroup object,
* which provides access to the currency and date functions as well as the other
* domains that the system has for translations.
*
* @return string|DomainGroup
*/
function _t() {
static $domains = null;
#If there are no domains we need to set them up first
if ($domains === null) { $domains = new DomainGroup(); }
#Get the functions arguments afterwards
$args = func_get_args();
#If the first parameter is a Locale, then we proceed to registering it so it'll
#provide translations for the programs
if (isset($args[0]) && $args[0] instanceof Locale) {
$locale = array_shift($args);
$domain = array_shift($args);
return $domains->putDomain($domain, new Domain($domain, $locale));
}
#If the args is empty, then we give return the domains that allow for printing
#and localizing of the data.
if (empty($args)) {
return $domains;
}
return call_user_func_array(Array($domains->getDefault(), 'say'), $args);
}
function current_context(ContextInterface$set = null) {
static $context = null;
if ($set!==null) {$context = $set;}
return $context;
}
function console() {
static $console = null;
if ($console === null) {
$console = new Console();
}
return $console;
}
function validate($target = null) {
$targets = array_filter(is_array($target)? $target : func_get_args());
if (!empty($targets) && reset($targets) instanceof ValidatorInterface) {
$messages = Array();
#Retrieve the messages from the validators
foreach ($targets as $target) {
$messages = array_merge($messages, $target->getMessages());
}
if (!empty($messages)) { throw new ValidationException('Validation failed', 1604200115, $messages); }
return $targets;
} else {
$validator = new Validator();
$validator->setValue($target);
return $validator;
}
}
/**
* Retrieves the current path from the request. This will retrieve the path
* without query string or document root.
*
* @see http://www.spitfirephp.com/wiki/index.php/NgiNX_Configuration For NGiNX setup
* @return string
*/
function getPathInfo() {
$base_url = spitfire()->baseUrl();
list($path) = explode('?', substr($_SERVER['REQUEST_URI'], strlen($base_url)));
if (strlen($path) !== 0) { return $path; }
else { return '/'; }
}
function _def(&$a, $b) {
return ($a)? $a : $b;
}
/**
* This function is a shorthand for "new Collection" which also allows fluent
* usage of the collection in certain environments where the PHP version still
* limits that behavior.
*
* @param mixed $elements
* @return Collection
*/
function collect($elements = []) {
return new Collection($elements);
}
/**
* Creates a new URL. Use this class to generate dynamic URLs or to pass
* URLs as parameters. For consistency (double base prefixes and this
* kind of misshaps aren't funny) use this object to pass or receive URLs
* as paramaters.
*
* Please note that when passing a URL that contains the URL as a string like
* "/hello/world?a=b&c=d" you cannot pass any other parameters. It implies that
* you already have a full URL.
*
* You can pass any amount of parameters to this class,
* the constructor will try to automatically parse the URL as good as possible.
* <ul>
* <li>Arrays are used as _GET</li>
* <li>App objects are used to identify the namespace</li>
* <li>Strings that contain / or ? will be parsed and added to GET and path</li>
* <li>The rest of strings will be pushed to the path.</li>
* </ul>
*/
function url() {
#Get the parameters the first time
$sf = spitfire();
$params = func_get_args();
#Extract the app
if (reset($params) instanceof App || $sf->appExists(reset($params))) {
$app = $sf->getApp(array_shift($params));
}
else {
$app = $sf;
}
#Get the controller, and the action
$controller = null;
$action = null;
$object = Array();
#Get the object
while(!empty($params) && (!is_array(reset($params)) || (!$controller && $app->getControllerLocator()->hasController(reset($params))))) {
if (!$controller) { $controller = array_shift($params); }
elseif (!$action) { $action = array_shift($params); }
else { $object[] = array_shift($params); }
}
#Get potential environment variables that can be used for additional information
#like loccalization
$get = array_shift($params);
$environment = array_shift($params);
return new URL($app, $controller, $action, $object, 'php', $get, $environment);
}
/**
* The within function is a math function that allows to determine whether a
* value is within a range and returns either the value, or the closest range
* delimiter.
*
* The first and the last parameter delimit the range. The second parameter is
* the one being tested.
*
* <code>within(1, 50, 100); //Outputs: 50</code>
* <code>within(1, 500, 100); //Outputs: 100</code>
* <code>within(1, -50, 100); //Outputs: 1</code>
*
* @param number $min
* @param number $val
* @param number $max
* @return number
*/
function within($min, $val, $max) {
return min(max($min, $val), $max);
}
function media() {
static $dispatcher = null;
if (!$dispatcher) {
- $dispatcher = new \spitfire\io\media\MediaDispatcher();
- $dispatcher->register('image/png', new \spitfire\io\media\GDManipulator());
- $dispatcher->register('image/jpg', new \spitfire\io\media\GDManipulator());
- $dispatcher->register('image/psd', new \spitfire\io\media\ImagickManipulator());
- $dispatcher->register('image/gif', new \spitfire\io\media\FFMPEGManipulator());
- $dispatcher->register('video/mp4', new \spitfire\io\media\FFMPEGManipulator());
- $dispatcher->register('video/quicktime', new \spitfire\io\media\FFMPEGManipulator());
- $dispatcher->register('image/jpeg', new \spitfire\io\media\GDManipulator());
- $dispatcher->register('image/vnd.adobe.photoshop', new \spitfire\io\media\ImagickManipulator());
+ $dispatcher = new MediaDispatcher();
+ $dispatcher->register('image/png', new GDManipulator());
+ $dispatcher->register('image/jpg', new GDManipulator());
+ $dispatcher->register('image/psd', new ImagickManipulator());
+ $dispatcher->register('image/gif', new FFMPEGManipulator());
+ $dispatcher->register('video/mp4', new FFMPEGManipulator());
+ $dispatcher->register('video/quicktime', new FFMPEGManipulator());
+ $dispatcher->register('image/jpeg', new GDManipulator());
+ $dispatcher->register('image/vnd.adobe.photoshop', new ImagickManipulator());
}
return $dispatcher;
}
/**
*
* @staticvar type $dispatcher
* @param type $uri
- * @return \spitfire\storage\objectStorage\DriveDispatcher|spitfire\storage\objectStorage\NodeInterface
+ * @return DriveDispatcher|NodeInterface
*/
function storage($uri = null) {
static $dispatcher = null;
if (!$dispatcher) {
- $dispatcher = new \spitfire\storage\objectStorage\DriveDispatcher();
- $dispatcher->register(new \spitfire\storage\drive\MountPoint('file://', '/'));
- $dispatcher->register(new \spitfire\storage\drive\MountPoint('app://', basedir()));
- $dispatcher->register(new \spitfire\storage\drive\MountPoint('temp://', sys_get_temp_dir()));
+ $dispatcher = new DriveDispatcher();
+ $dispatcher->register(new MountPoint('file://', '/'));
+ $dispatcher->register(new MountPoint('app://', basedir()));
+ $dispatcher->register(new MountPoint('temp://', sys_get_temp_dir()));
}
if ($uri) {
return $dispatcher->get($uri);
}
else {
return $dispatcher;
}
}
function request($url) {
- return new \spitfire\io\curl\Request($url);
+ return new Request($url);
}
function mime($file) {
if (function_exists('mime_content_type')) { return mime_content_type($file); }
else { return explode(';', system(sprintf('file -bi %s', escapeshellarg(realpath($file)))))[0]; }
}
function debug() {
static $instance = null;
- return $instance? $instance : $instance = php_sapi_name() === 'cli'? new \spitfire\exceptions\ExceptionHandlerCLI() : new \spitfire\exceptions\ExceptionHandler();
+ return $instance? $instance : $instance = php_sapi_name() === 'cli'? new ExceptionHandlerCLI() : new ExceptionHandler();
+}
+
+/**
+ * Allows the application to manage a central event dispatching system, instead
+ * of relying on every component to build a custom one. If your component doesn't
+ * wish to share it's hooks and plugins please @see Target
+ *
+ * @staticvar Target $plugins
+ * @return PublisherTree
+ */
+function event() {
+ static $plugins = null;
+
+ if ($plugins === null) {
+ $plugins = new PublisherTree();
+ \spitfire\core\event\RecipeLoader::import();
+ }
+
+ return $plugins;
}
function basedir() {
return BASEDIR;
}
\ No newline at end of file
diff --git a/tests/core/event/TargetTest.php b/tests/core/event/PublisherTest.php
similarity index 86%
rename from tests/core/event/TargetTest.php
rename to tests/core/event/PublisherTest.php
index 69ddbb9..d76d188 100644
--- a/tests/core/event/TargetTest.php
+++ b/tests/core/event/PublisherTest.php
@@ -1,50 +1,50 @@
<?php namespace tests\spitfire\core\event;
use PHPUnit\Framework\TestCase;
/*
* The MIT License
*
* Copyright 2019 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
-class TargetTest extends TestCase
+class PublisherTest extends TestCase
{
private $plugins;
public function setUp() : void {
- $this->plugins = new \spitfire\core\event\Target();
- $this->plugins->test->after()->do(function ($e) { return $e + 1; });
- $this->plugins->test->n1->before()->do(function ($e) { return $e + 1; });
+ $this->plugins = new \spitfire\core\event\PublisherTree();
+ $this->plugins->test->after()->subscribe(function ($e) { return $e + 1; });
+ $this->plugins->test->n1->before()->subscribe(function ($e) { return $e + 1; });
parent::setUp();
}
public function testTarget() {
$r = $this->plugins->test->do(function ($e) { $this->assertEquals(1, $e); }, 1);
$this->assertEquals(2, $r);
$r = $this->plugins->test->n1->do(function ($e) { $this->assertEquals(2, $e); }, 1);
$this->assertEquals(2, $r);
}
}
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Thu, Apr 15, 8:56 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2071
Default Alt Text
(27 KB)

Event Timeline