/** * @param string $name * @param array $request * @param array $responseKeys * @throws Exception * @return object */ public function execute($name, $request, $responseKeys) { $start = microtime(1); $fce = saprfc_function_discover($this->connection, $name); if (!$fce) { throw new Exception("Error discovering " . $name, 1); } foreach ($request as $k => $v) { if (is_array($v)) { foreach ($v as $index => $row) { if (is_object($row)) { $row = get_object_vars($row); } foreach ($row as $row_key => $row_value) { $row[$row_key] = $this->encodeString($row_value); } saprfc_table_insert($fce, $k, $row, $index + 1); } } else { saprfc_request($fce, $k, $this->encodeString($v)); } } $result = saprfc_call_and_receive($fce); if ($result != SAPRFC_OK) { $message = isset($this->errors[$result]) ? $this->errors[$result] : 'Unknown error'; if ($this->profiler) { $this->profiler->register((object) array('name' => $name, 'request' => $request, 'success' => false, 'message' => $message, 'time' => microtime(1) - $start)); } throw new Exception($message); } $response = array(); foreach ($responseKeys as $table) { $count = saprfc_table_rows($fce, $table); if ($count == -1) { // responseKeys param $data = $this->decodeString(saprfc_export($fce, $table)); } else { // responseKeys table $data = array(); for ($i = 1; $i <= $count; $i++) { $row = saprfc_table_read($fce, $table, $i); foreach ($row as $k => $v) { $row[$k] = $this->decodeString($v); } $data[] = (object) $row; } } $response[$table] = $data; } if ($this->profiler) { $this->profiler->register((object) array('name' => $name, 'request' => (object) $request, 'response' => (object) $response, 'success' => true, 'time' => microtime(1) - $start)); } return (object) $response; }
/** * Trigger an event call to the application. * * @access public * @param string $event The event to trigger within the appliction. * @param array $params Parameters to pass to the application. * @static */ public static function trigger($event, $params) { // Start the profiler Profiler::register('Core', 'Event.' . $event); // Create a reference to the users event listener $listener = Config::get('settings', 'project') . '\\EventListener'; // Call the appropriate event listener function $listener::$event($params); // Stop the profiler Profiler::deregister('Core', 'Event.' . $event); }
/** * Initialises the application configuration and runs the router. * * @access public * @param string $projectName The name of the project the user wishes to run. * @param Core\Router $router The routes the users application requires. */ public function __construct($projectName, $router = null) { // Start the profiler Profiler::start(); Profiler::register('Core', 'Front'); // Load the configuration for this project Profiler::register('Core', 'Config'); Config::load($projectName); Profiler::deregister('Core', 'Config'); Profiler::register('Core', 'Request'); Request::setUrl(); Profiler::deregister('Core', 'Request'); // Set the project information $this->_projectName = $projectName; $this->_router = $router ?: new Router(); // And route $this->route(); }
/** * Provides a nice interface to call view helpers. * * This is a magic function, so any calls to the view/view helper which do not * exist will end up here. We only pass through the first parameter to make for * a nicer implementation in each view helper. This is why it needs to be an array. * * @access public * @param string $helperName The View Helper that we wish to use. * @param array $param The parameters that need to be passed to the View Helper. * @return string * @magic */ public function __call($helperName, $param) { // Try and instantiate the helper // Note: Calling section_Author will translate to Section\Author $viewHelperClassName = Config::get('settings', 'project') . '\\View\\Helper\\' . str_replace('_', '\\', $helperName); $viewHelper = new $viewHelperClassName(); // Render and return Profiler::register('Helper', $helperName); $content = $viewHelper->render(isset($param[0]) ? $param[0] : array()); Profiler::deregister('Helper', $helperName); return $content; }
/** * Load a controllers action, and ask the View to render it. * * @access public * @param object $controller Controller object that we want to load the action for. * @param string $action Name of the action we wish to load. * @static */ public static function loadAction($controller, $action) { // Start the profiler Profiler::register('Core', 'Dispatcher'); // In order to have pretty URL's we allow the basic routing to contain // .. dashes in their action names which will be removed here. It allows // .. /index/hello-world to be routed to /index/helloworld. $action = str_replace('-', '', $action); $actionExists = is_callable(array($controller, $action . 'Action')); $controllerName = str_replace(Config::get('settings', 'project') . '\\Controller\\', '', get_class($controller)); // Make sure that the controller has the action if (!$actionExists) { // If this is the error controller then there is no hope if ($controllerName == 'Error') { die('Sorry, an error occurred whilst processing your request.'); } else { if ($action != 'error') { Profiler::deregister('Core', 'Dispatcher'); Dispatcher::loadAction($controller, 'error'); } else { Profiler::deregister('Core', 'Dispatcher'); Profiler::deregister('Controller', $controllerName); Dispatcher::loadController('Error', 'notFound'); } } } // We are able to call the controllers action Event::trigger('initAction', array('controller' => $controller, 'action' => $action)); Profiler::deregister('Core', 'Dispatcher'); Profiler::register('Action', $action); // Set the controller and action that we are heading to $controller->view->controller = $controllerName; $controller->view->action = $action; // Call and render this action $controller->{$action . 'Action'}(); $controller->view->render(); }
/** * Start the routing procedure and find a valid route, if any. * * @access public */ public function route() { // Start the profiler Profiler::register('Core', 'Router'); // First, let's look at the URL the user supplied $requestUrl = array_values(array_filter(explode('/', Request::getUrl()))); $requestRoute = null; // Loop over each route and test to see if they are valid foreach (self::$_routes as $route) { if ($this->routeTest($requestUrl, $route)) { $requestRoute = $route; break; } } // We have completed the route matching // Finish the setup of the request object Profiler::register('Core', 'Request'); if ($requestRoute) { $_GET['controller'] = $route->endpoint['controller']; $_GET['action'] = $route->endpoint['action']; Request::setUrlFragments(str_replace($this->_routePath, '', Request::getUrl())); } else { Request::setUrlFragments(Request::getUrl(), true); } Profiler::deregister('Core', 'Request'); // Inform the event listener a request has been initialised Event::trigger('initRequest', array('controller' => Request::get('controller'), 'action' => Request::get('action'))); // And stop the profiler Profiler::deregister('Core', 'Router'); Profiler::deregister('Core', 'Front'); // And dispatch Dispatcher::loadController(Request::get('controller'), Request::get('action')); }