/** * Start SamsonPHP framework. * * @param string $default Default module identifier * * @throws ViewPathNotFound */ public function start($default) { // TODO: Change ExternalModule::init() signature // Fire core started event Event::fire('core.started'); // TODO: Does not see why it should be here // Set main template path $this->template($this->template_path); // Security layer $securityResult = true; // Fire core security event // Event::fire('core.security', array(&$this, &$securityResult)); /** @var mixed $result External route controller action result */ $result = false; // If we have passed security application layer if ($securityResult) { // Fire core routing event - go to routing application layer Event::signal('core.routing', array(&$this, &$result, $default)); } // If no one has passed back routing callback if (!isset($result) || $result === false) { // Fire core e404 - routing failed event $result = Event::signal('core.e404', array(url()->module, url()->method)); } // Response $output = ''; // If this is not asynchronous response and controller has been executed if (!$this->async && $result !== false) { // Store module data $data = $this->active->toView(); // Render main template $output = $this->render($this->template_path, $data); // Fire after render event Event::fire('core.rendered', array(&$output)); } // Output results to client echo $output; // Fire ended event Event::fire('core.ended', array(&$output)); }
/** * Load module from path to core. * * @param string $path Path for module loading * @param array $parameters Collection of loading parameters * * @return $this|bool */ public function load($path, $parameters = array()) { // Check path if (file_exists($path)) { /** @var ResourceMap $resourceMap Gather all resources from path */ $resourceMap = ResourceMap::get($path); if (isset($resourceMap->module[0])) { /** @var string $controllerPath Path to module controller file */ $controllerPath = $resourceMap->module[1]; /** @var string $moduleClass Name of module controller class to load */ $moduleClass = $resourceMap->module[0]; // Require module controller class into PHP if (file_exists($controllerPath)) { require_once $controllerPath; } // TODO: this should be done via composer autoload file field // Iterate all function-style controllers and require them foreach ($resourceMap->controllers as $controller) { require_once $controller; } /** @var ExternalModule $connector Create module controller instance */ $connector = new $moduleClass($path, $resourceMap, $this); // Set composer parameters $connector->composerParameters = $parameters; // Get module identifier $moduleID = $connector->id(); // Fire core module load event Event::fire('core.module_loaded', array($moduleID, &$connector)); // Signal core module configure event Event::signal('core.module.configure', array(&$connector, $moduleID)); // TODO: Think how to decouple this // Call module preparation handler if (!$connector->prepare()) { // Handle module failed preparing } // Trying to find parent class for connecting to it to use View/Controller inheritance $parentClass = get_parent_class($connector); if (!in_array($parentClass, array('samson\\core\\ExternalModule', 'samson\\core\\CompressableExternalModule'))) { // Переберем загруженные в систему модули foreach ($this->module_stack as &$m) { // Если в систему был загружен модуль с родительским классом if (get_class($m) == $parentClass) { $connector->parent =& $m; //elapsed('Parent connection for '.$moduleClass.'('.$connector->uid.') with '.$parent_class.'('.$m->uid.')'); } } } } elseif (is_array($parameters) && isset($parameters['samsonphp_package_compressable']) && $parameters['samsonphp_package_compressable'] == 1) { /** @var \samson\core\ExternalModule $connector Create module controller instance */ $connector = new VirtualModule($path, $resourceMap, $this, str_replace('/', '', $parameters['module_id'])); // Set composer parameters $connector->composerParameters = $parameters; } else { // Signal error return e('Cannot load module from: "##"', E_SAMSON_FATAL_ERROR, $path); } } else { return e('Cannot load module from[##]', E_SAMSON_FATAL_ERROR, $path); } // Chaining return $this; }
/** * Prepare modules * * @param array $modules * @param $container */ protected function prepareModules(array $modules, $container) { foreach ($modules as $module) { $identifier = $module->name; if ($module->className) { // Fix samson.php files if (!class_exists($module->className)) { require_once $module->pathName; } $instance = $container->get($module->className); } else { continue; } // Set composer parameters $instance->composerParameters = $module->composerParameters; // TODO: Change event signature to single approach // Fire core module load event Event::fire('core.module_loaded', [$identifier, &$instance]); // Signal core module configure event Event::signal('core.module.configure', [&$instance, $identifier]); if ($instance instanceof PreparableInterface) { // Call module preparation handler if (!$instance->prepare()) { // throw new \Exception($identifier.' - Module preparation stage failed'); } } $instance->parent = $this->getClassParentModule($container, get_parent_class($instance)); } }
define('__SAMSON_BASE__', '/' . basename(__SAMSON_CWD__) . '/'); } /** Set default locale to - Russian */ if (!defined('DEFAULT_LOCALE')) { define('DEFAULT_LOCALE', 'ru'); } /** Require composer autoloader */ if (!class_exists('samson\\core\\Core')) { require_once '../vendor/autoload.php'; } /** Automatic parent web-application configuration read */ if (file_exists('../../../app/config')) { /** Special constant to disable local ActiveRecord configuration */ define('EXTERNAL_CONFIG', true); // Signal core configure event \samsonphp\event\Event::signal('core.configure', array('../../../' . __SAMSON_CONFIG_PATH, __SAMSON_PUBLIC_PATH . __SAMSON_BASE__)); } // Set supported locales setlocales('en', 'ua', 'ru'); // Start SamsonPHP application s()->composer()->subscribe('core.e404', 'default_e404')->subscribe('core.routing', array(url(), 'router')); /** Automatic external SamsonCMS Application searching */ if (file_exists('../../../src/')) { // Get reource map to find all modules in src folder foreach (\samson\core\ResourceMap::get('../../../src/')->modules as $module) { // We are only interested in SamsonCMS application ancestors if (in_array('samson\\cms\\App', class_parents($module[2])) !== false) { // Remove possible '/src/' path from module path if (($pos = strripos($module[1], '/src/')) !== false) { $module[1] = substr($module[1], 0, $pos); }
public function testUnsubscribe() { // Add two subscribers $identifier = \samsonphp\event\Event::subscribe('test.unsubscribe', array($this, 'eventDynamicCallback')); $param = 'test'; $result = \samsonphp\event\Event::signal('test.unsubscribe', array(&$param)); // Perform test - only first subscriber must be executed $this->assertEquals(2, $result); \samsonphp\event\Event::unsubscribe('test.unsubscribe', $identifier); $result = \samsonphp\event\Event::signal('test.unsubscribe', array(&$param)); $this->assertEquals(null, $result); }