/** * Gets a temporary instance of a Dispatcher * * @param string $option The component name * @param string $view The View name * @param array $config Configuration data * * @return ADispatcher */ public static function &getTmpInstance($option = null, $view = null, $config = array()) { if (array_key_exists('input', $config)) { if ($config['input'] instanceof AInput) { $input = $config['input']; } else { if (!is_array($config['input'])) { $config['input'] = (array) $config['input']; } $config['input'] = array_merge($_REQUEST, $config['input']); $input = new AInput($config['input']); } } else { $input = new AInput(); } $defaultApp = AApplication::getInstance()->getName(); if (!is_null($option)) { $config['option'] = $option; } else { $config['option'] = $input->getCmd('option', $defaultApp); } if (!is_null($view)) { $config['view'] = $view; } else { $config['view'] = $input->getCmd('view', ''); } $input->set('option', $config['option']); $input->set('view', $config['view']); $config['input'] = $input; $className = ucfirst($config['option']) . 'Dispatcher'; if (!class_exists($className)) { $basePath = APATH_INSTALLATION; $searchPaths = array($basePath . '/' . $config['option'] . '/platform', $basePath . '/' . $config['option'] . '/platform/dispatchers', $basePath . '/' . $config['option'], $basePath . '/' . $config['option'] . '/dispatchers'); if (array_key_exists('searchpath', $config)) { array_unshift($searchPaths, $config['searchpath']); } $path = AUtilsPath::find($searchPaths, 'dispatcher.php'); if ($path) { require_once $path; } } if (!class_exists($className) && class_exists($className . 'Default')) { $className = $className . 'Default'; } elseif (!class_exists($className)) { $className = 'ADispatcher'; } $instance = new $className($config); return $instance; }
/** * Constructor * * @param array $config A named configuration array for object construction.<br/> * name: the name (optional) of the view (defaults to the view class name suffix).<br/> * escape: the name (optional) of the function to use for escaping strings<br/> * base_path: the parent path (optional) of the views directory (defaults to the component folder)<br/> * template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name<br/> * helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/)<br/> * layout: the layout (optional) to use to display the view<br/> * * @param \AContainer $container * * @throws \AExceptionApp * @throws \Exception */ public function __construct($config = array(), AContainer $container = null) { if (is_null($container)) { $container = AApplication::getInstance()->getContainer(); } $this->container = $container; $this->input = $this->container->input; // Get the component name if (array_key_exists('option', $config)) { if ($config['option']) { $component = $config['option']; } } $config['option'] = $component; // Get the view name $view = $this->input->getCmd('view', ''); if (array_key_exists('view', $config)) { if ($config['view']) { $view = $config['view']; } } $config['view'] = $view; // Set the component and the view to the input array $this->input->set('option', $config['option']); $this->input->set('view', $config['view']); // Set the view name if (array_key_exists('name', $config)) { $this->_name = $config['name']; } else { $this->_name = $config['view']; } $this->input->set('view', $this->_name); $config['input'] = $this->input; $config['name'] = $this->_name; $config['view'] = $this->_name; // Set a base path for use by the view if (array_key_exists('base_path', $config)) { $this->_basePath = $config['base_path']; } else { $this->_basePath = APATH_INSTALLATION . '/' . $config['option']; } // Set the default template search path if (array_key_exists('template_path', $config)) { // User-defined dirs $this->_setPath('template', $config['template_path']); } else { $this->_setPath('template', $this->_basePath . '/views/' . $this->getName() . '/tmpl'); } // Set the default helper search path if (array_key_exists('helper_path', $config)) { // User-defined dirs $this->_setPath('helper', $config['helper_path']); } else { $this->_setPath('helper', $this->_basePath . '/helpers'); } // Set the layout if (array_key_exists('layout', $config)) { $this->setLayout($config['layout']); } else { $this->setLayout('default'); } $this->config = $config; $app = $this->container->application; $component = preg_replace('/[^A-Z0-9_\\.-]/i', '', $component); $fallback = APATH_THEMES . '/' . $app->getTemplate() . '/html/' . $component . '/' . $this->getName(); $this->_addPath('template', $fallback); $this->baseurl = AUri::base(true); }
/** * Public constructor of the Controller class * * @param array $config Optional configuration parameters * @param AContainer $container Application container */ public function __construct($config = array(), AContainer $container = null) { if (is_null($container)) { $container = AApplication::getInstance()->getContainer(); } $this->container = $container; // Initialise $this->methods = array(); $this->message = null; $this->messageType = 'message'; $this->paths = array(); $this->redirect = null; $this->taskMap = array(); // Get the input $this->input = $this->container->input; // Determine the methods to exclude from the base class. $xMethods = get_class_methods('AController'); // Get the public methods in this class using reflection. $r = new ReflectionClass($this); $rMethods = $r->getMethods(ReflectionMethod::IS_PUBLIC); foreach ($rMethods as $rMethod) { $mName = $rMethod->getName(); // Add default display method if not explicitly declared. if (!in_array($mName, $xMethods) || $mName == 'display' || $mName == 'main') { $this->methods[] = strtolower($mName); // Auto register the methods as tasks. $this->taskMap[strtolower($mName)] = $mName; } } // Get the default values for the component and view names $defaultApp = $this->container->application->getName(); $this->component = $this->input->get('option', $defaultApp, 'cmd'); $this->view = $this->input->get('view', 'cpanel', 'cmd'); $this->layout = $this->input->get('layout', null, 'cmd'); // Overrides from the config if (array_key_exists('option', $config)) { $this->component = $config['option']; } if (array_key_exists('view', $config)) { $this->view = $config['view']; } if (array_key_exists('layout', $config)) { $this->layout = $config['layout']; } $this->input->set('option', $this->component); // Set the $name variable $this->name = $this->component; // Set the basePath variable $basePath = APATH_INSTALLATION . '/' . $this->component; if (array_key_exists('base_path', $config)) { $basePath = $config['base_path']; } $this->basePath = $basePath; // If the default task is set, register it as such if (array_key_exists('default_task', $config)) { $this->registerDefaultTask($config['default_task']); } else { $this->registerDefaultTask('main'); } // Set the models prefix if (empty($this->model_prefix)) { if (array_key_exists('model_prefix', $config)) { // User-defined prefix $this->model_prefix = $config['model_prefix']; } else { $this->model_prefix = ucfirst($this->name) . 'Model'; } } // Set the default view search path if (array_key_exists('view_path', $config)) { // User-defined dirs $this->setPath('view', $config['view_path']); } else { $this->setPath('view', $this->basePath . '/views'); } // Set the default view. if (array_key_exists('default_view', $config)) { $this->default_view = $config['default_view']; } elseif (empty($this->default_view)) { $this->default_view = $this->getName(); } // Cache the config $this->config = $config; // Set any model/view name overrides if (array_key_exists('viewName', $config)) { $this->setThisViewName($config['viewName']); } if (array_key_exists('modelName', $config)) { $this->setThisModelName($config['modelName']); } }
/** * Creates a View object instance and returns it * * @param string $name The name of the view, e.g. Items * @param string $prefix The prefix of the view, e.g. FoobarView * @param string $type The type of the view, usually one of Html, Raw, Json or Csv * @param array $config The configuration variables to use for creating the view * * @return FOFView */ protected function createView($name, $prefix = '', $type = '', $config = array()) { $result = null; // Clean the view name $viewName = preg_replace('/[^A-Z0-9_]/i', '', $name); $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); $viewType = preg_replace('/[^A-Z0-9_]/i', '', $type); if ($config['input'] instanceof AInput) { $tmpInput = $config['input']; } else { $tmpInput = new AInput($config['input']); } // Guess the component name and view if (!empty($prefix)) { preg_match('/(.*)View$/', $prefix, $m); $component = strtolower($m[1]); } else { $component = ''; } if (empty($component) && array_key_exists('input', $config)) { $component = $tmpInput->get('option', $component, 'cmd'); } if (array_key_exists('option', $config)) { if ($config['option']) { $component = $config['option']; } } $config['option'] = $component; $view = strtolower($viewName); if (empty($view) && array_key_exists('input', $config)) { $view = $tmpInput->get('view', $view, 'cmd'); } if (array_key_exists('view', $config)) { if ($config['view']) { $view = $config['view']; } } $config['view'] = $view; if (array_key_exists('input', $config)) { $tmpInput->set('option', $config['option']); $tmpInput->set('view', $config['view']); $config['input'] = $tmpInput; } // Get the base paths where the view class files are expected to live $basePaths = array(APATH_INSTALLATION . '/' . $config['option'] . '/platform/views', APATH_INSTALLATION . '/' . $config['option'] . '/views'); $basePaths = array_merge($basePaths, $this->paths['view']); $suffixes = array($viewName, 'default'); foreach ($suffixes as $suffix) { // Build the view class name $viewClass = $classPrefix . ucfirst($suffix); if (class_exists($viewClass)) { // The class is already loaded break; } elseif (class_exists($viewClass . 'Default')) { // The default class was loaded successfully. We have a match! $viewClass = $viewClass . 'Default'; break; } // The class is not loaded. Let's load it! $viewPath = $this->createFileName('view', array('name' => $suffix, 'type' => $viewType)); $path = AUtilsPath::find($basePaths, $viewPath); if ($path) { require_once $path; } if (class_exists($viewClass)) { // The class is already loaded break; } elseif (class_exists($viewClass . 'Default')) { // The default class was loaded successfully. We have a match! $viewClass = $viewClass . 'Default'; break; } } if (!class_exists($viewClass)) { //$viewClass = 'AView'.ucfirst($type); $viewClass = 'AView'; } // Setup View configuration options $basePath = APATH_INSTALLATION; if (!array_key_exists('template_path', $config)) { $config['template_path'] = array($basePath . '/' . $config['option'] . '/platform/views/' . $config['view'] . '/tmpl', $basePath . '/' . $config['option'] . '/views/' . $config['view'] . '/tmpl'); } if (!array_key_exists('helper_path', $config)) { $config['helper_path'] = array($basePath . '/' . $config['option'] . '/platform/helpers', $basePath . '/' . $config['option'] . '/helpers'); } $result = new $viewClass($config); return $result; }