public static function register() { Event::listen('autoloader.realpath', function (&$path) { $path = str_replace('//', '/', $path); $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $path = \system\Core\Module\StreamWrapper::intercept($path, 'realpath'); }); StreamWrapper::wrap(); }
// default method that will be called // do some stuff print "default handle( ) method called<br><br>"; } public function myMethod($data) { // do some stuff print "custom method has been called<br><br>"; } } /* REGISTERING THE CLASS WITH THE METHOD HANDLE( ) */ PtcEvent::listen('form.success', 'TableModel'); /* REGISTERING THE CLASS WITH A CUSTOM METHOD */ PtcEvent::listen('form.error', 'TableModel@myMethod'); /* FIRING THE EVENTS */ PtcEvent::fire('form.submit', array('form data')); PtcEvent::fire('form.error', array('form data')); /* FIRING EVENT WITH THE DATA REFERENCED */ $data = 'form data'; print "The data value: " . $data . '<br>'; PtcEvent::fire('ptc.query', array(&$data)); // adding "&" references to manipulate the data print 'Data changed thanks to the "&" reference: ' . $data . '<br><br>'; /* GETTING THE CURRENT EVENT LISTENERS */ print "<b>The current event listeners:</b><pre>"; print print_r(PtcEvent::getEvents(), true) . "</pre>"; /* REMOVING LISTENERS */ PtcEvent::remove('form.error'); // removing the last added event PtcEvent::remove('form.error', 0); // removing the first event by key
/* STOP CODE COVERAGE ANALYSIS */ ptc_stop_coverage(); // PtcDebug::stopCoverage( ) /* STOP FUNCTION CALLS TRACING */ ptc_stop_trace(); // PtcDebug::stopTrace( ) /*** PTC HANDYMAN HELPERPS ****************************************************/ /* ADDING APPLICATION PATHS FOR LATER USAGE ( PtcHandMan::addAppPath( ) ) */ ptc_add_path(array('lib' => dirname(__FILE__) . '/autoloader-example-files')); /* ADDING CLASS FILES ( PtcHandMan::addFile( ) ) */ ptc_add_file(array('HmTestClassFile' => ptc_path('lib') . '/class-file.php', 'ns\\HmTestClassFile' => ptc_path('lib') . '/ns-class-file.php')); /* ADDING DIRECTORIES WITH CLASSES TO THE AUTOLOADER ( PtcHandMan::addDir( ) ) */ ptc_add_dir(ptc_path('lib')); // PtcHandMan::getAppPath( /* ADDING A NAMESPACED DIRECTORY WITH CLASSES TO THE AUTOLOADER */ ptc_add_dir(array('nsTest' => ptc_path('lib') . '/namespaceTest')); /* GETTING THE DIRECTORIES OF THE AUTOLOADER ( PtcHandyMan::getDirs( ) )*/ $dirs = ptc_dir(); // PtcHandyMan::getDirs( ) params: ( files , directories , ns ) ptc_log($dirs, 'getting all directories and files to be autoloaded'); //PtcDebug::bufferLog( ); /*** PTC EVENT HELPERPS ****************************************************/ /* ADDING EVENT LISTENERS ( PtcEvent::listen( ) ) */ ptc_listen('some.event', function ($data) { // do some stuff ptc_log($data, 'Called event with closure as call back'); // PtcDebug::bufferLog( ) }); /* FIRING EVENTS ( PtcEvent::fire( ) ) */ PtcEvent::fire('some.event', array('some data'));
/** * Registers the autoloader to load classes when needed. See @ref hm_getting_started * @param bool $addThisPath adds the path where the class resides as a directory * @param bool $useHelpers load the ptc-helpers.php file if found, and registers the event class * @param bool $registerAutoLoader registers the load method with the spl utilities */ public static function register($addThisPath = true, $useHelpers = true, $registerAutoLoader = true) { $this_class = get_called_class(); if ($addThisPath) { static::addDir(dirname(__FILE__)); } // add this path if ($registerAutoLoader) { spl_autoload_register(array($this_class, 'load')); } if ($useHelpers && file_exists(dirname(__FILE__) . '/ptc-helpers.php')) { require_once dirname(__FILE__) . '/ptc-helpers.php'; } if ($useHelpers && file_exists(dirname(__FILE__) . '/PtcEvent.php')) { __NAMESPACE__ . PtcEvent::register(); // register PtcEvent with helpers } $namespace = @strtoupper(@str_replace('\\', '_', __NAMESPACE__)) . '_'; @define('_PTCHANDYMAN_' . $namespace, $this_class); // declare the class namespace static::_debug('Autoloader registerd', '', 'Autoloader'); }
/** * */ protected static function _shutdown($response, $print, $obj) { $event = Event::getEvent('app'); if (is_array($event) && ptc_array_get($event, 'stop', false)) { ptc_fire('app.stop', array($obj, &$response)); } if (!$print) { return $response; } echo $response; }