Created
July 2, 2011 15:56
-
-
Save alkavan/1060945 to your computer and use it in GitHub Desktop.
kohana events
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 defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Plugin Controller Template | |
* @file \modules\plugin\classes\controller\template\plugin.php | |
*/ | |
abstract class Controller_Template_Plugin extends Controller_Template | |
{ | |
public $event_markdown = TRUE; | |
public function before() | |
{ | |
parent::before(); | |
require_once Kohana::find_file('vendor', 'prggmr/lib/prggmr'); | |
if($this->event_markdown === TRUE) | |
{ | |
$this->_subscribe_events(); | |
} | |
} | |
public function after() | |
{ | |
parent::after(); | |
} | |
protected function _subscribe_events() | |
{ | |
// Header Events | |
Event::subscribe('EventBeforeHeader', function ($event) { | |
echo '<!-- EventBeforeHeader -->'; | |
}, | |
null, 0); | |
Event::subscribe('EventAfterHeader', function ($event) { | |
echo '<!-- EventAfterHeader -->'; | |
}, | |
null, 999); | |
// Content events | |
Event::subscribe('EventBeforeContent', function ($event) { | |
echo '<!-- EventBeforeContent -->'; | |
}, | |
null, 0); | |
Event::subscribe('EventAfterContent', function ($event) { | |
echo '<!-- EventAfterContent -->'; | |
}, | |
null, 999); | |
// Footer Events | |
Event::subscribe('EventBeforeFooter', function ($event) { | |
echo '<!-- EventBeforeFooter -->'; | |
}, | |
null, 0); | |
Event::subscribe('EventAfterFooter', function ($event) { | |
echo '<!-- EventAfterFooter -->'; | |
}, | |
null, 999); | |
} | |
} | |
// End Controller_Template_Plugin | |
?> | |
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Plugin Event | |
* @file \modules\plugin\classes\event.php | |
*/ | |
class Event | |
{ | |
/** | |
* Attaches a new subscription to a signal queue. | |
* | |
* NOTE: Passing an array as the signal parameter should be done only | |
* once per subscription que as each time a new Queue is created. | |
* | |
* | |
* @param mixed $signal Signal the subscription will attach to, this | |
* can be a Signal object, the signal representation or an array | |
* for a chained signal. | |
* | |
* @param mixed $subscription Subscription closure that will trigger on | |
* fire or a Subscription object. | |
* | |
* @param mixed $identifier String identifier for this subscription, if | |
* an integer is provided it will be treated as the priority. | |
* | |
* @param mixed $priority Priority of this subscription within the Queue | |
* | |
* @throws InvalidArgumentException Thrown when an invalid callback is | |
* provided. | |
* | |
* @return void | |
*/ | |
static public function subscribe($signal, $subscription, $identifier = null, $priority = null) | |
{ | |
subscribe($signal, $subscription, $identifier = null, $priority = null); | |
} | |
static public function fire($signal, $vars = null, $event = null) | |
{ | |
fire($signal, $vars = null, $event = null); | |
} | |
} | |
// End Controller_Template_Plugin | |
?> | |
<body class=""> | |
<div id="wrapper" class=""> | |
<div id="wrapper_inner" class=""> | |
<div id="header"> | |
<?php fire('EventBeforeHeader'); ?> | |
<?php echo $header ?> | |
<?php fire('EventAfterHeader'); ?> | |
</div> | |
<div id="nav"> | |
<?php echo $nav ?> | |
</div> | |
<div id="content" class=""> | |
<?php fire('EventBeforeContent'); ?> | |
<?php echo $content ?> | |
<?php fire('EventAfterContent'); ?> | |
</div> | |
<div class="clear"></div> | |
<div id="footer"> | |
<?php fire('EventBeforeFooter'); ?> | |
<?php echo $footer ?> | |
<?php fire('EventBeforeFooter'); ?> | |
</div> | |
</div> | |
</div> | |
<?php if ( ! IN_PRODUCTION) echo View::factory('profiler/stats') ?> | |
</body> | |
</html> | |
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Test Controller (CURRENT: events) | |
* | |
* @author Igal Alkon <[email protected]> | |
*/ | |
class Controller_Test extends Controller_Template_Site | |
{ | |
/** | |
* Test Index | |
* @return void | |
*/ | |
public function action_index() | |
{ | |
// add something to header | |
Event::subscribe('EventBeforeHeader', function ($event) { | |
echo View::factory('test/plugin_header')->render(); | |
}, | |
null, 5); | |
Event::subscribe('EventBeforeHeader', function ($event) { | |
echo '(defined second!)'; | |
}, | |
null, 1); | |
Event::subscribe('EventAfterHeader', function ($event) { | |
echo '(after third!)'; | |
}, | |
null, 5); | |
// add something to footer | |
Event::subscribe('EventBeforeFooter', function ($event) { | |
echo View::factory('test/plugin_footer')->render(); | |
}, | |
null, 1); | |
} | |
} // End Controller_Test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment