示例#1
0
 protected static function factory($class)
 {
     if (!self::$instance) {
         self::$instance = new $class();
     }
     return self::$instance;
 }
示例#2
0
 public static function app_from_uri($uri)
 {
     $uri = moojon_uri::clean_uri($uri);
     $route_match = moojon_routes::map($uri);
     $data = $route_match->get_params();
     foreach ($data as $key => $value) {
         moojon_request::set($key, $value);
     }
     $app = $data['app'];
     self::require_view_functions();
     require_once moojon_paths::get_app_path($app);
     $app_class = self::get_app_class($app);
     return new $app_class($uri);
 }
示例#3
0
 public final function set_location($uri)
 {
     $uri = moojon_uri::clean_uri($uri);
     $route_match = moojon_routes::map($uri);
     $data = $route_match->get_params();
     $this->app_name = $data['app'];
     $this->controller_name = $data['controller'];
     $this->action_name = $data['action'];
     if (self::get_app_class($this->app_name) != get_class($this)) {
         $location = moojon_config::get('index_file') . $uri;
         $this->redirect($location);
     }
     $this->set_controller();
 }
示例#4
0
 protected function __construct()
 {
     $uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : $_SERVER['PATH_INFO'];
     $this->uri = self::clean_uri($uri);
     if ($match = moojon_routes::map($this->uri)) {
         $config = array_merge(moojon_config::get_data(), moojon_config::read(moojon_paths::get_project_config_environment_app_directory(ENVIRONMENT, $match->get('app'))));
         if (array_key_exists('secure', $config) && $config['secure'] === true && !moojon_security::authenticate()) {
             moojon_cache::disable();
             $pattern = ':app/:controller/:action';
             $match = new moojon_route_match($pattern, array_merge($match->get_params(), array('app' => $config['security_app'], 'controller' => $config['security_controller'], 'action' => $config['security_action'])), new moojon_route($pattern));
             $this->uri = $config['security_app'] . '/' . $config['security_controller'] . '/' . $config['security_action'];
         }
         $this->match = $match;
         $this->data = $this->match->get_params();
         $this->route = $this->match->get_route();
         self::try_define('APP', $this->data['app']);
         self::try_define('CONTROLLER', $this->data['controller']);
         self::try_define('ACTION', $this->data['action']);
     } else {
         self::_404($this->uri);
     }
 }
示例#5
0
 public static function scaffold($app, $model, $controller)
 {
     self::try_define('APP', $app);
     self::try_define('CONTROLLER', $controller);
     if (moojon_routes::has_rest_route($model)) {
         $route = moojon_routes::get_rest_route($model);
         if (APP != $route->get_app()) {
             throw new moojon_exception("Scaffold app & route app must be the same (" . APP . ' != ' . $route->get_app() . ")");
         }
         $id_property = $route->get_id_property();
     } else {
         $id_property = moojon_primary_key::NAME;
         self::add_route("new moojon_rest_route('{$model}', array('app' => '" . APP . "')),");
     }
     $swaps = array();
     $swaps['plural'] = moojon_inflect::pluralize($model);
     $swaps['singular'] = moojon_inflect::singularize($model);
     $swaps['Human'] = str_replace('_', ' ', ucfirst(moojon_inflect::singularize($model)));
     $swaps['human'] = str_replace('_', ' ', moojon_inflect::singularize($model));
     $swaps['Humans'] = str_replace('_', ' ', ucfirst(moojon_inflect::pluralize($model)));
     $swaps['humans'] = str_replace('_', ' ', moojon_inflect::pluralize($model));
     $swaps['id_property'] = $id_property;
     $views_path = moojon_paths::get_project_views_app_controller_directory(APP, CONTROLLER);
     moojon_files::attempt_mkdir($views_path);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'controller.template', moojon_paths::get_project_controllers_app_directory(APP, CONTROLLER) . "{$controller}.controller.class.php", $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . '_form.template', $views_path . '_form.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'new.view.template', $views_path . 'new.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'delete.view.template', $views_path . 'delete.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'index.view.template', $views_path . 'index.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'show.view.template', $views_path . 'show.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'edit.view.template', $views_path . 'edit.view.php', $swaps, false, true);
 }
示例#6
0
function get_primary_key_id_property(moojon_base_model $model)
{
    $resource = moojon_inflect::pluralize(get_class($model));
    if (moojon_routes::has_rest_route($resource)) {
        $rest_route = moojon_routes::get_rest_route($resource);
        return $rest_route->get_id_property();
    } else {
        return moojon_primary_key::NAME;
    }
}
示例#7
0
function rest_actions(moojon_base_model $model, moojon_rest_route $route = null, $action = null, $attributes = array())
{
    $action = $action ? $action : strtolower(ACTION);
    $route = $route ? $route : moojon_routes::get_rest_route($model->get_table());
    $actions = $route->get_actions();
    $lis = array();
    switch ($action) {
        case 'index':
            if (in_array('_new', $actions)) {
                $lis[] = li_tag(new_member_tag($model));
            }
            break;
        case '_new':
            if (in_array('index', $actions)) {
                $lis[] = li_tag(collection_tag($model));
            }
            break;
        case 'show':
            if (in_array('edit', $actions)) {
                $lis[] = li_tag(edit_member_tag($model));
            }
            if (in_array('delete', $actions)) {
                $lis[] = li_tag(delete_member_tag($model));
            }
            break;
        case 'edit':
            if (in_array('show', $actions)) {
                $lis[] = li_tag(member_tag($model));
            }
            if (in_array('delete', $actions)) {
                $lis[] = li_tag(delete_member_tag($model));
            }
            break;
        case 'delete':
            if (in_array('show', $actions)) {
                $lis[] = li_tag(member_tag($model));
            }
            if (in_array('edit', $actions)) {
                $lis[] = li_tag(edit_member_tag($model));
            }
            break;
        default:
            $lis = array();
            break;
    }
    return div_tag(ul_tag($lis), $attributes);
}
示例#8
0
 public static final function get_rest_route_resources()
 {
     $rest_route_resources = array();
     foreach (moojon_routes::get_rest_routes() as $rest_route) {
         $rest_route_resources[] = $rest_route->get_pattern();
     }
     return $rest_route_resources;
 }
 private function get_relationship_routes()
 {
     $relationship_routes = array();
     $model_class = moojon_inflect::singularize($this->resource);
     if ($path = moojon_paths::get_model_path($model_class)) {
         require_once $path = moojon_paths::get_model_path($model_class);
         $model = new $model_class();
         foreach ($model->get_relationships() as $relationship) {
             $foreign_table = $relationship->get_foreign_table();
             if (moojon_routes::has_rest_route($foreign_table)) {
                 $relationship_route = moojon_routes::get_rest_route($foreign_table);
             } else {
                 $relationship_route = new moojon_rest_route($foreign_table, array_merge($this->params, array('controller' => $foreign_table)));
             }
             $relationship_routes[] = $relationship_route;
         }
     }
     return $relationship_routes;
 }