Skip to content
Snippets Groups Projects
Commit 414008da authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

add events notifications

parent 67e32fa4
Branches
Tags
1 merge request!26add events notifications
Pipeline #10508 passed with stage
in 43 seconds
......@@ -26,18 +26,25 @@ THE SOFTWARE.
class Storm_Collection extends ArrayObject {
public function addAll($collection) {
foreach($collection as $element) {
$this->append($element);
}
foreach($collection as $element)
$this->add($element);
return $collection;
}
public function add($anObject) {
$this->append($anObject);
return $this;
}
public function newInstance($elements) {
$classname = get_class($this);
return new $classname($elements);
}
public function collect($closure) {
return $this->newInstance(array_map($closure,
(array)$this));
......
<?php
/*
STORM is under the MIT License (MIT)
Copyright (c) 2010-2020 Agence Française Informatique http://www.afi-sa.fr
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 Storm_Event_Save {
protected $_model;
public function __construct($model) {
$this->_model = $model;
}
public function getModel() {
return $this->_model;
}
}
<?php
/*
STORM is under the MIT License (MIT)
Copyright (c) 2010-2020 Agence Française Informatique http://www.afi-sa.fr
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 Storm_Events {
protected static $_instance;
protected $_observers;
/** @category testing */
public static function setInstance($instance) {
static::$_instance = $instance;
}
public static function getInstance() {
return static::$_instance
? static::$_instance
: static::$_instance = new static();
}
public function __construct() {
$this->_observers = new Storm_Set();
}
/**
* @param $observer callable
*/
public function register($observer) {
$this->_observers->add($observer);
return $this;
}
/**
* @param $observer callable
*/
public function unregister($observer) {
$this->_observers = $this->_observers
->reject(function($each) use($observer)
{
return $each === $observer;
});
return $this;
}
/**
* @param $classname name of a class of observer
*/
public function unregisterClass($classname) {
$this->_observers = $this->_observers
->reject(function($each) use($classname)
{
return is_object($each) && get_class($each) == $classname;
});
return $this;
}
/**
* @param $event Storm_Event
*/
public function notify($event) {
$this->_observers->eachDo(function($observer) use($event)
{
$observer($event);
});
return $this;
}
}
......@@ -268,13 +268,19 @@ abstract class Storm_Model_Abstract {
$this->_updateNullBelongsToIdFieldsFromDependents();
if ($valid = $this->isValid()) {
$this->saveWithoutValidation();
$this->_attributes_in_db=$this->_attributes;
$this->_getEvents()->notify(new Storm_Event_Save($this));
$this->_attributes_in_db = $this->_attributes;
}
return $valid;
}
protected function _getEvents() {
return Storm_Events::getInstance();
}
public function assertSave() {
if (!$this->save())
throw new Storm_Model_Exception('Can\'t save '.get_class($this).': '.implode(',',$this->getErrors()));
......
<?php
/*
STORM is under the MIT License (MIT)
Copyright (c) 2010-2020 Agence Française Informatique http://www.afi-sa.fr
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 Storm_Set extends Storm_Collection {
public function add($anObject) {
if (!$this->includes($anObject))
parent::add($anObject);
return $this;
}
}
<?php
/*
STORM is under the MIT License (MIT)
Copyright (c) 2010-2020 Agence Française Informatique http://www.afi-sa.fr
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.
*/
abstract class Storm_Model_EventTriggeringTestCase extends Storm_Test_ModelTestCase {
protected
$_events,
$_callable,
$_called = false;
public function setUp() {
parent::setUp();
Storm_Model_Loader::defaultToVolatile();
$this->_events = Storm_Events::getInstance();
}
public function tearDown() {
Storm_Model_Loader::defaultToDb();
parent::tearDown();
}
/** @test */
public function withRegisteredClosureShouldCallBackOnSave() {
$this->_events->register($this->_callable);
(new Storm_Model_EventTriggeringModel)->save();
$this->assertTrue($this->_called);
}
/** @test */
public function withUnregisteredClosureShouldNotCallbackOnSave() {
$this->_events->register($this->_callable)
->unregister($this->_callable);
(new Storm_Model_EventTriggeringModel)->save();
$this->assertFalse($this->_called);
}
}
class Storm_Model_EventTriggeringClosureTest extends Storm_Model_EventTriggeringTestCase {
public function setUp() {
parent::setUp();
$this->_callable = function($event) { $this->_called = true; };
}
}
class Storm_Model_EventTriggeringCallableArrayTest extends Storm_Model_EventTriggeringTestCase {
public function setUp() {
parent::setUp();
$this->_callable = [$this, 'onSave'];
}
public function onSave($event) {
$this->_called = true;
}
}
class Storm_Model_EventTriggeringInvokableTest extends Storm_Model_EventTriggeringTestCase {
public function setUp() {
parent::setUp();
$this->_callable = new Storm_Model_EventTriggeringInvokable($this);
}
public function beCalled() {
$this->_called = true;
}
/** @test */
public function withUnregisteredByClassNameShouldNotCallbackOnSave() {
$this->_events->register($this->_callable)
->unregisterClass(Storm_Model_EventTriggeringInvokable::class);
(new Storm_Model_EventTriggeringModel)->save();
$this->assertFalse($this->_called);
}
}
class Storm_Model_EventTriggeringModel extends Storm_Model_Abstract {}
class Storm_Model_EventTriggeringInvokable {
protected $_test;
public function __construct($test) {
$this->_test = $test;
}
public function __invoke($event) {
$this->_test->beCalled();
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment