private function dispatch($args) { MvcConfiguration::set('ExecutionContext', 'shell'); echo Console_Color::convert("\n%P%UWelcome to WP MVC Console!%n%n\n\n"); $shell_name = 'help'; if (!empty($args[1])) { $shell_name = $args[1]; } $shell_title = MvcInflector::camelize($shell_name); $shell_name .= '_shell'; $shell_class_name = MvcInflector::camelize($shell_name); $shell_path = 'shells/' . $shell_name . '.php'; $shell_exists = $this->file_includer->include_first_app_file_or_core_file($shell_path); if (!$shell_exists) { echo 'Sorry, a shell named "' . $shell_name . '" couldn\'t be found in any of the MVC plugins.'; echo "\n"; echo 'Please make sure a shell class exists in "app/' . $shell_path . '", or execute "./wpmvc" to see a list of available shells.'; echo "\n"; die; } $args = array_slice($args, 2); if (empty($args[0])) { $args = array('main'); } $shell = new $shell_class_name($args); $method = $args[0]; $args = array_slice($args, 1); if ($shell_name != 'help_shell') { $shell->out(Console_Color::convert("\n%_[Running " . $shell_title . "::" . $method . "]%n")); } $shell->{$method}($args); if ($shell_name != 'help_shell') { $shell->out(Console_Color::convert("\n%_[Complete]%n")); } }
public function add_menu_pages() { global $_registered_pages; $menu_position = 12; $menu_position = apply_filters('mvc_menu_position', $menu_position); $admin_pages = MvcConfiguration::get('AdminPages'); foreach ($this->admin_controller_names as $controller_name) { if (isset($admin_pages[$controller_name])) { if (empty($admin_pages[$controller_name]) || !$admin_pages[$controller_name]) { continue; } $pages = $admin_pages[$controller_name]; } else { $pages = null; } $processed_pages = $this->process_admin_pages($controller_name, $pages); $hide_menu = isset($pages['hide_menu']) ? $pages['hide_menu'] : false; if (!$hide_menu) { $menu_icon = 'dashicons-admin-generic'; /* check if there is a corresponding model with a menu_icon post type argument */ try { $model_name = MvcInflector::singularize(MvcInflector::camelize($controller_name)); $model = mvc_model($model_name); if (isset($model->wp_post['post_type']['args']['menu_icon'])) { $menu_icon = $model->wp_post['post_type']['args']['menu_icon']; } } catch (Exception $e) { //not every controller must have a corresponding model, continue silently } $controller_titleized = MvcInflector::titleize($controller_name); $admin_controller_name = 'admin_' . $controller_name; $top_level_handle = 'mvc_' . $controller_name; $method = $admin_controller_name . '_index'; $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $admin_controller_name . '", "action" => "index"));'); $capability = $this->admin_controller_capabilities[$controller_name]; add_menu_page($controller_titleized, $controller_titleized, $capability, $top_level_handle, array($this->dispatcher, $method), $menu_icon, $menu_position); foreach ($processed_pages as $key => $admin_page) { $method = $admin_controller_name . '_' . $admin_page['action']; if (!method_exists($this->dispatcher, $method)) { $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $admin_controller_name . '", "action" => "' . $admin_page['action'] . '"));'); } $page_handle = $top_level_handle . '-' . $key; $parent_slug = empty($admin_page['parent_slug']) ? $top_level_handle : $admin_page['parent_slug']; if ($admin_page['in_menu']) { add_submenu_page($parent_slug, $admin_page['label'] . ' ‹ ' . $controller_titleized, $admin_page['label'], $admin_page['capability'], $page_handle, array($this->dispatcher, $method)); } else { // It looks like there isn't a more native way of creating an admin page without // having it show up in the menu, but if there is, it should be implemented here. // To do: set up capability handling and page title handling for these pages that aren't in the menu $hookname = get_plugin_page_hookname($page_handle, ''); if (!empty($hookname)) { add_action($hookname, array($this->dispatcher, $method)); } $_registered_pages[$hookname] = true; } } $menu_position++; } } }
function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $object_id = empty($options['id']) ? null : $options['id']; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $params = $_REQUEST; $params = self::escape_params($params); if (is_admin()) { unset($params['page']); } else { if (empty($params['id']) && !empty($object_id)) { $params['id'] = $object_id; } } $controller->params = $params; $controller->set('this', $controller); $controller->{$action}(); $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action); } }
private function generate_model($plugin, $name) { $plugin_app_path = $this->get_plugin_app_path($plugin); $name_camelized = MvcInflector::camelize($name); $name_underscored = MvcInflector::underscore($name); $target_path = $plugin_app_path . 'models/' . $name_underscored . '.php'; $vars = array('name' => $name_camelized); $this->templater->create('model', $target_path, $vars); }
static function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $params = $options; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $request_params = $_REQUEST; $request_params = self::escape_params($request_params); $params = array_merge($request_params, $params); if (is_admin()) { unset($params['page']); } $controller->params = $params; $controller->set('this', $controller); if (!empty($controller->before)) { foreach ($controller->before as $method) { $controller->{$method}(); } } // If we can access the response from our controller's actions (methods) // we can use them (the actions) as Widgets in the both sides - outside of wp-mvc plugin and inside of it. // The action should return whatever you want, except $this->render_view('view_name'). // Example: Let's say we have 'UserController' and action 'list' and we want somewhere in Wordpress or another // plugin to get all users and to reuse UserController list method, so: // class UserController extends MvcPublicController { // public function list() { // $this->view_rendered = true; // $this->set_objects(); // $response = this->render_to_string('users/list'); // return $response; // } // } // and where we want to reuse it, we can get it in the following way: // $widget = MvcDispatcher::dispatch(array( // 'controller' => 'users', // 'action' => 'list' // )); $response = $controller->{$action}(); if (!empty($controller->after)) { foreach ($controller->after as $method) { $controller->{$method}(); } } $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action, $options); } return $response; }
static function add_object($key, &$object) { $_this =& MvcObjectRegistry::get_instance(); $key = MvcInflector::camelize($key); if (!isset($_this->__objects[$key])) { $_this->__objects[$key] =& $object; return true; } return false; }
public function add_settings($key, &$settings) { $_this =& self::get_instance(); $key = MvcInflector::camelize($key); if (!isset($_this->__settings[$key])) { $_this->__settings[$key] = $settings; return true; } return false; }
public function add_model($key, &$model) { $_this =& self::get_instance(); $key = MvcInflector::camelize($key); if (!isset($_this->__models[$key])) { $_this->__models[$key] = $model; return true; } return false; }
public function public_url($options = array()) { $routes = self::get_public_routes(); $controller = $options['controller']; $action = empty($options['action']) ? 'index' : $options['action']; $matched_route = null; if (!empty($options['object']) && is_object($options['object'])) { $model_name = MvcInflector::camelize(MvcInflector::singularize($controller)); $model = MvcModelRegistry::get_model($model_name); if (!empty($model) && method_exists($model, 'to_url')) { $url = site_url('/'); $url .= $model->to_url($options['object']); return $url; } if (empty($options['id']) && !empty($options['object']->__id)) { $options['id'] = $options['object']->__id; } } foreach ($routes as $route) { $route_path = $route[0]; $route_defaults = $route[1]; if (!empty($route_defaults['controller']) && $route_defaults['controller'] == $controller) { if (!empty($route_defaults['action']) && $route_defaults['action'] == $action) { $matched_route = $route; } } } $url = site_url('/'); if ($matched_route) { $path_pattern = $matched_route[0]; preg_match_all('/{:([\\w]+).*?}/', $path_pattern, $matches, PREG_SET_ORDER); $path = $path_pattern; foreach ($matches as $match) { $pattern = $match[0]; $option_key = $match[1]; if (isset($options[$option_key])) { $value = $options[$option_key]; $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1); } } $path = rtrim($path, '/') . '/'; $url .= $path; } else { $url .= $options['controller'] . '/'; if (!empty($options['action']) && $options['action'] != 'show') { $url .= $options['action'] . '/'; } if (!empty($options['id'])) { $url .= $options['id'] . '/'; } } return $url; }
function mvc_render_to_string($view, $vars = array()) { $view_pieces = explode('/', $view); $model_tableized = $view_pieces[0]; $model_camelized = MvcInflector::camelize($model_tableized); $controller_name = $model_camelized . 'Controller'; if (!class_exists($controller_name)) { $controller_name = 'MvcPublicController'; } $controller = new $controller_name(); $controller->init(); $controller->set($vars); $string = $controller->render_to_string($view); return $string; }
private function dispatch($args) { MvcConfiguration::set('ExecutionContext', 'shell'); if (empty($args[1])) { MvcError::fatal('Please provide the name of the shell as the first argument.'); } $shell_name = $args[1]; $shell_name .= '_shell'; $shell_class_name = MvcInflector::camelize($shell_name); $this->file_includer->require_first_app_file_or_core_file('shells/' . $shell_name . '.php'); $args = array_slice($args, 2); if (empty($args[0])) { $args = array('main'); } $shell = new $shell_class_name($args); $method = $args[0]; $args = array_slice($args, 1); $shell->{$method}($args); }
/** * Get a list of the available shells. * Also is executed if the wpmvc console is run with no arguments. * * @param mixed $args */ public function main($args) { $shells = $this->get_available_shells(); $this->out('Available Shells:'); $table = new Console_Table(CONSOLE_TABLE_ALIGN_LEFT, ' ', 1, null, true); foreach ($shells as $plugin => $shells) { $plugin_label = MvcInflector::camelize(MvcInflector::underscore($plugin)); for ($i = 0; $i < count($shells); $i++) { if ($i > 0) { $plugin_label = ' '; } $shell_name = MvcInflector::camelize($shells[$i]); $table->addRow(array($plugin_label, Console_Color::convert('%_' . $shell_name . '%n'))); } $table->addSeparator(); } $this->out($table->getTable()); $this->out('To get information about a shell try:'); $this->out("\n\twpmvc help shell <name_of_shell>"); }
function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $params = $options; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $request_params = $_REQUEST; $request_params = self::escape_params($request_params); $params = array_merge($request_params, $params); if (is_admin()) { unset($params['page']); } $controller->params = $params; $controller->set('this', $controller); if (!empty($controller->before)) { foreach ($controller->before as $method) { $controller->{$method}(); } } $controller->{$action}(); if (!empty($controller->after)) { foreach ($controller->after as $method) { $controller->{$method}(); } } $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action, $options); } }
public function __get($property_name) { if (!empty($this->__settings['properties'][$property_name])) { $property = $this->__settings['properties'][$property_name]; if ($property['type'] == 'association') { $objects = $this->get_associated_objects($property['association']); $this->{$property_name} = $objects; return $this->{$property_name}; } } if ($this->__settings['model']['name'] == 'MvcPost') { if (!empty($this->post_type)) { if (substr($this->post_type, 0, 4) == 'mvc_') { $model_name = MvcInflector::camelize(substr($this->post_type, 4)); $model = MvcModelRegistry::get_model($model_name); $object = $model->find_one(array('post_id' => $this->ID, 'recursive' => 0)); $this->{$property_name} = $object; return $this->{$property_name}; } } } $class = empty($this->__model_name) ? 'MvcModelObject' : $this->__model_name; MvcError::warning('Undefined property: ' . $class . '::' . $property_name . '.'); }
protected function init_default_columns() { if (empty($this->default_columns)) { MvcError::fatal('No columns defined for this view. Please define them in the controller, like this: <pre> class ' . MvcInflector::camelize($this->name) . 'Controller extends MvcAdminController { var $default_columns = array(\'id\', \'name\'); } </pre>'); } $admin_columns = array(); foreach ($this->default_columns as $key => $value) { if (is_array($value)) { if (!isset($value['label'])) { $value['label'] = MvcInflector::titleize($key); } } else { if (is_integer($key)) { $key = $value; if ($value == 'id') { $value = array('label' => 'ID'); } else { $value = array('label' => MvcInflector::titleize($value)); } } else { $value = array('label' => $value); } } $value['key'] = $key; $admin_columns[$key] = $value; } $this->default_columns = $admin_columns; }
private function input_id($field_name) { return $this->model_name . MvcInflector::camelize($field_name); }
public function public_url($options = array()) { $options = apply_filters('mvc_before_public_url', $options); $defaults = array('action' => 'index', 'controller' => null); $options = array_merge($defaults, $options); $routes = self::get_public_routes(); $controller = $options['controller']; $action = $options['action']; $matched_route = null; if (!empty($options['object']) && is_object($options['object'])) { if (!empty($options['object']->__model_name) && !$controller) { $model_name = $options['object']->__model_name; $controller = MvcInflector::tableize($model_name); } else { if ($controller) { $model_name = MvcInflector::camelize(MvcInflector::singularize($controller)); } else { MvcError::warning('Incomplete arguments for MvcRouter::public_url().'); } } $model = MvcModelRegistry::get_model($model_name); if (!empty($model) && method_exists($model, 'to_url')) { $url = site_url('/'); $method = new ReflectionMethod(get_class($model), 'to_url'); $parameter_count = $method->getNumberOfParameters(); if ($parameter_count == 2) { $url .= $model->to_url($options['object'], $options); } else { $url .= $model->to_url($options['object']); } return $url; } if (empty($options['id']) && !empty($options['object']->__id)) { $options['id'] = $options['object']->__id; } } foreach ($routes as $route) { $route_path = $route[0]; $route_defaults = $route[1]; if (!empty($route_defaults['controller']) && $route_defaults['controller'] == $controller) { if (!empty($route_defaults['action']) && $route_defaults['action'] == $action) { $matched_route = $route; } } } $url = site_url('/'); if ($matched_route) { $path_pattern = $matched_route[0]; preg_match_all('/{:([\\w]+).*?}/', $path_pattern, $matches, PREG_SET_ORDER); $path = $path_pattern; foreach ($matches as $match) { $pattern = $match[0]; $option_key = $match[1]; if (isset($options[$option_key])) { $value = $options[$option_key]; $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1); } else { if (isset($options['object']) && is_object($options['object'])) { if (isset($options['object']->{$option_key})) { $value = $options['object']->{$option_key}; $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1); $path = rtrim($path, '.*?'); } } } } $path = rtrim($path, '/') . '/'; $url .= $path; } else { $url .= $controller . '/'; if (!empty($action) && !in_array($action, array('show', 'index'))) { $url .= $action . '/'; } if (!empty($options['id'])) { $url .= $options['id'] . '/'; } } return $url; }
public static function class_name_from_filename($filename) { return MvcInflector::camelize(str_replace('.php', '', $filename)); }
protected function load_models() { $models = array(); foreach ($this->plugin_app_paths as $plugin_app_path) { $model_filenames = $this->file_includer->require_php_files_in_directory($plugin_app_path . 'models/'); foreach ($model_filenames as $filename) { $models[] = MvcInflector::class_name_from_filename($filename); } } $this->model_names = array(); foreach ($models as $model) { $this->model_names[] = $model; $model_class = MvcInflector::camelize($model); $model_instance = new $model_class(); MvcModelRegistry::add_model($model, $model_instance); } }