Example #1
0
function __autoload($class)
{
    $class_path = moojon_paths::get_class_path($class);
    $class_paths = array($class_path);
    if (!$class_path) {
        $class_path = moojon_paths::get_model_path($class);
        $class_paths[] = $class_path;
    }
    if (!$class_path) {
        $class_path = moojon_paths::get_base_model_path($class);
        $class_paths[] = $class_path;
    }
    if (!$class_path) {
        $class_path = moojon_paths::get_interface_path($class);
        $class_paths[] = $class_path;
    }
    if (!$class_path) {
        $class_path = moojon_paths::get_column_path($class);
        $class_paths[] = $class_path;
    }
    if ($class_path) {
        require_once $class_path;
    } else {
        throw new moojon_exception("Not found ({$class})");
    }
}
 private function get_migrations()
 {
     $migrations = array();
     foreach (moojon_files::directory_files(moojon_paths::get_project_migrations_directory()) as $migration) {
         $migration_file = moojon_migrator::get_migration_class_name($migration);
         $migrations[] = substr($migration_file, 0, strlen($migration_file) - 10);
     }
     return $migrations;
 }
Example #3
0
 protected function __construct()
 {
     foreach (require_once moojon_paths::get_routes_path() as $route) {
         if (is_subclass_of($route, 'moojon_base_route')) {
             $this->data[$route->get_pattern()] = $route;
         } else {
             throw new moojon_exception("Only moojon_base_route derived objects may be included in {$routes_path} (" . get_class($route) . ' found)');
         }
     }
 }
 private function get_installed_pluggins()
 {
     $installed_pluggins = array();
     foreach (moojon_files::directory_directories(moojon_paths::get_project_pluggins_directory(), false, true) as $pluggin_directory) {
         if (file_exists($pluggin_directory . 'installed')) {
             $installed_pluggins[] = basename($pluggin_directory);
         }
     }
     return $installed_pluggins;
 }
Example #5
0
function partial($partial, $variables = array())
{
    $backtrace = debug_backtrace();
    $view_directory = dirname($backtrace[0]['file']) . '/';
    $controller = basename($view_directory, '/');
    $app = basename(dirname($view_directory));
    if (!($partial_path = moojon_paths::get_partial_path($app, $controller, $partial))) {
        throw new moojon_exception("Unknown partial ({$partial})");
    }
    moojon_runner::partial($partial_path, $variables);
}
Example #6
0
 public static function render($path, $variables = array(), $uri = null)
 {
     if (moojon_cache::get_enabled() && $uri) {
         $cache_path = moojon_paths::get_cache_path($uri);
         if (moojon_cache::expired($uri)) {
             moojon_files::put_file_contents($cache_path, self::expose($path, $variables));
         }
         return moojon_files::get_file_contents($cache_path);
     } else {
         return self::expose($path, $variables);
     }
 }
Example #7
0
 public final function render()
 {
     if (!$this->controller) {
         $this->set_controller();
     }
     $return = moojon_runner::render(moojon_paths::get_view_path(self::get_app_name($this), self::get_controller_name($this->controller), $this->controller->get_view()), $this->controller, $this->uri);
     $layout = $this->get_layout();
     if ($layout !== false) {
         $return = str_replace('YIELD', $return, moojon_runner::render(moojon_paths::get_layout_path($layout), $this->controller), $this->uri);
     }
     return $return;
 }
Example #8
0
 protected function __construct()
 {
     $this->data = (require_once MOOJON_DIRECTORY . 'config/moojon.config.php');
     $project_config_path = moojon_paths::get_project_config_directory() . 'project.config.php';
     if (defined('PROJECT_DIRECTORY') && file_exists($project_config_path)) {
         $this->data = array_merge($this->data, require_once $project_config_path);
     }
     $config_environment = moojon_paths::get_project_config_environment_directory(ENVIRONMENT) . 'environment.config.php';
     if (defined('PROJECT_DIRECTORY') && is_file($config_environment)) {
         foreach (require_once $config_environment as $key => $value) {
             $this->data[$key] = $value;
         }
     }
     date_default_timezone_set($this->data['timezone']);
 }
Example #9
0
 public static function run()
 {
     self::find_or_create_schema_migrations_table();
     $migrations = array();
     foreach (schema_migration::read(null, 'version') as $migration) {
         $migrations[] = $migration->version;
     }
     $migration_files = moojon_files::directory_files(moojon_paths::get_project_migrations_directory());
     sort($migration_files);
     foreach ($migration_files as $migration_file) {
         if (!in_array(basename($migration_file), $migrations)) {
             self::run_migration($migration_file, 'up');
         }
     }
 }
Example #10
0
function img_uri($uri)
{
    $uri_segments = parse_url($uri);
    $path_segments = pathinfo($uri_segments['path']);
    if (!array_key_exists('extension', $path_segments)) {
        $path_segments['extension'] = moojon_config::get('default_image_ext');
    }
    if (!array_key_exists('dirname', $path_segments) || $path_segments['dirname'] == '.') {
        $path_segments['dirname'] = moojon_paths::get_public_images_directory();
    }
    if (substr($path_segments['dirname'], -1) == '/') {
        $path_segments['dirname'] = substr($path_segments['dirname'], 0, -1);
    }
    $uri_segments['path'] = $path_segments['dirname'] . '/' . $path_segments['filename'] . '.' . $path_segments['extension'];
    return process_uri($uri_segments['path']);
}
Example #11
0
 public static function render()
 {
     if (ENVIRONMENT != 'production') {
         $div = new moojon_div_tag(null, array('id' => 'debug'));
         $div->add_child(new moojon_h2_tag('Session'));
         $ul = new moojon_ul_tag();
         foreach ($_SESSION as $key => $value) {
             if (is_array($value)) {
                 $value = 'array(' . implode(', ', $value) . ')';
             }
             $ul->add_child(new moojon_li_tag("{$key}: {$value}"));
         }
         $div->add_child($ul);
         $div->add_child(new moojon_h2_tag('Cookie'));
         $ul = new moojon_ul_tag();
         foreach ($_COOKIE as $key => $value) {
             if (is_array($value)) {
                 $value = 'array(' . implode(', ', $value) . ')';
             }
             $ul->add_child(new moojon_li_tag("{$key}: {$value}"));
         }
         $div->add_child($ul);
         $div->add_child(new moojon_h2_tag('URL'));
         $ul = new moojon_ul_tag();
         $app = APP;
         $controller = CONTROLLER;
         $action = ACTION;
         $ul->add_child(new moojon_li_tag("App: {$app}"));
         $ul->add_child(new moojon_li_tag("Controller: {$controller}"));
         $ul->add_child(new moojon_li_tag("Action: {$action}"));
         $div->add_child($ul);
         $div->add_child(new moojon_h2_tag('Paths'));
         $ul = new moojon_ul_tag();
         $app_path = moojon_paths::get_app_path($app);
         $controller_path = moojon_paths::get_controller_path($app, $controller);
         $view_path = moojon_paths::get_view_path($app, $controller, $action);
         $ul->add_child(new moojon_li_tag("app_path: {$app_path}"));
         $ul->add_child(new moojon_li_tag("controller_path: {$controller_path}"));
         $ul->add_child(new moojon_li_tag("view_path: {$view_path}"));
         $div->add_child($ul);
         echo $div->render();
     }
 }
Example #12
0
function uploaded_file_tag(moojon_base_model $model, $column_name, $mime_type_column = null)
{
    $value = $model->{$column_name};
    if ($value) {
        if (!$mime_type_column) {
            $mime_type_column = $column_name . '_' . moojon_config::get('mime_type_column');
        }
        $mime_type = $model->has_column($mime_type_column) ? $model->{$mime_type_column} : moojon_files::get_mime_content_type($value);
        $path = moojon_paths::get_public_column_upload_path($model, $column_name);
        if (substr($mime_type, 0, 5) == 'image') {
            $content = img_tag($path, $model);
        } else {
            $content = $path;
        }
        return a_tag($content, $path, array('target' => '_blank'));
    } else {
        return p_tag('Not set');
    }
}
Example #13
0
 public static function expired($path, $absolute = false, $cache_for = null)
 {
     if (!$cache_for) {
         $cache_for = moojon_config::get('cache_for');
     }
     if (!$absolute) {
         $path = moojon_paths::get_cache_path($path);
     }
     if (file_exists($path)) {
         if (time() > filectime($path) + $cache_for) {
             moojon_files::unlink($path);
             return true;
         } else {
             return false;
         }
     } else {
         return true;
     }
 }
Example #14
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);
     }
 }
 protected static final function validate_matches($matches, $validate = true)
 {
     if ($validate) {
         if (!array_key_exists('app', $matches) || !array_key_exists('controller', $matches) || !array_key_exists('action', $matches)) {
             return false;
         }
         if ($path = moojon_paths::get_app_path($matches['app'])) {
             require_once $path;
         } else {
             return false;
         }
         if ($path = moojon_paths::get_controller_path($matches['app'], $matches['controller'])) {
             require_once $path;
         } else {
             return false;
         }
         if (!method_exists(self::get_controller_class($matches['controller']), $matches['action']) && !moojon_paths::get_view_path($matches['app'], $matches['controller'], $matches['action'])) {
             return false;
         }
     }
     return true;
 }
Example #16
0
 public static function add_route($route)
 {
     $routes_path = moojon_paths::get_routes_path();
     $read_file_handle = fopen($routes_path, 'r');
     $routes = '';
     while ($line = fgets($read_file_handle)) {
         $routes .= "{$line}";
         if (preg_match("/return\\s+array\\s*\\(/", $line)) {
             $routes .= "\t{$route}\n";
         }
     }
     fclose($read_file_handle);
     $write_file_handle = fopen($routes_path, 'w');
     fwrite($write_file_handle, $routes);
     fclose($write_file_handle);
     echo "Adding route ({$route})\n";
 }
Example #17
0
 public static function get_actions()
 {
     $data = self::get_data();
     require_once moojon_paths::get_controller_path($data['app'], $data['controller']);
     $actions = get_class_methods(self::get_controller_class($data['controller']));
     $paths = array(moojon_paths::get_moojon_views_directory(), moojon_paths::get_moojon_views_app_directory(moojon_uri::get_app()), moojon_paths::get_moojon_views_app_controller_directory(moojon_uri::get_app(), moojon_uri::get_controller()), moojon_paths::get_project_views_directory(), moojon_paths::get_project_views_app_directory(moojon_uri::get_app()), moojon_paths::get_project_views_app_controller_directory(moojon_uri::get_app(), moojon_uri::get_controller()));
     foreach (self::colate_view($paths) as $view) {
         if (!in_array($actions, $view)) {
             $actions[] = $view;
         }
     }
     return $actions;
 }
 public function run($arguments)
 {
     $command = $this->prompt_until_in(array_shift($arguments), $this->get_commands(), 'What would you like to generate?');
     switch ($command) {
         case 'model':
             $tables = moojon_db::show_tables();
             if (!count($tables)) {
                 throw new moojon_exception('No tables to generate models from');
             }
             $arguments = self::check_arguments('moojon_generate_cli::model()', 1, $arguments);
             $table = $this->prompt_until_in($arguments[0], $tables, 'What table would you like to generate a model for?');
             moojon_generator::model($table);
             break;
         case 'models':
             $arguments = self::check_arguments('moojon_generate_cli::models()', 0, $arguments);
             moojon_generator::models();
             break;
         case 'migration':
             $arguments = self::check_arguments('moojon_generate_cli::migration()', 1, $arguments);
             $migration = $this->prompt_until($arguments[0], 'Please enter a migration name');
             moojon_generator::migration($migration);
             break;
         case 'app':
             $arguments = self::check_arguments('moojon_generate_cli::app()', 3, $arguments);
             $app = $this->prompt_until($arguments[0], 'Please enter an app name');
             $controller = $this->prompt_until($arguments[1], 'Please enter an app name', moojon_config::get('default_controller'));
             $action = $this->prompt_until($arguments[2], 'Please enter an action name', moojon_config::get('default_action'));
             moojon_generator::app($app, $controller, $action);
             break;
         case 'controller':
             $arguments = self::check_arguments('moojon_generate_cli::controller()', 3, $arguments);
             $app = $this->prompt_for_app($arguments[0]);
             $controller = $this->prompt_until($arguments[1], 'Please enter a controller name');
             $action = $this->prompt_until($arguments[2], 'Please enter an action name', 'index');
             moojon_generator::controller($app, $controller, $action);
             break;
         case 'helper':
             $arguments = self::check_arguments('moojon_generate_cli::helper()', 1, $arguments);
             $helper = $this->prompt_until($arguments[0], 'Please enter a name for this helper');
             moojon_generator::helper($helper);
             break;
         case 'layout':
             $arguments = self::check_arguments('moojon_generate_cli::layout()', 1, $arguments);
             $layout = $this->prompt_until($arguments[0], 'Please enter a layout name');
             moojon_generator::layout($layout);
             break;
         case 'view':
             $arguments = self::check_arguments('moojon_generate_cli::view()', 3, $arguments);
             $app = $this->prompt_for_app($arguments[0]);
             $controller = $this->prompt_for_controller($app, $arguments[1]);
             $view = $this->prompt_until($arguments[2], 'Please enter a view name');
             moojon_generator::view($app, $controller, $view);
             break;
         case 'partial':
             $arguments = self::check_arguments('moojon_generate_cli::partial()', 3, $arguments);
             $app = $this->prompt_for_app($arguments[0]);
             $controller = $this->prompt_for_controller($app, $arguments[1]);
             $partial = $this->prompt_until($arguments[2], 'Please enter a name for this partial');
             moojon_generator::partial($app, $controller, $partial);
             break;
         case 'scaffold':
             $arguments = self::check_arguments('moojon_generate_cli::scaffold()', 3, $arguments);
             $app = $this->prompt_for_app($arguments[0]);
             $models = array();
             foreach (moojon_files::directory_files(moojon_paths::get_project_models_directory()) as $path) {
                 $models[] = basename($path);
             }
             if (!count($models)) {
                 if ($this->prompt_until_in(null, array('y', 'n'), 'No models found. Would you like to generate models?') == 'y') {
                     moojon_generator::models();
                 } else {
                     throw new moojon_exception('Unable to generate scaffold (no models)');
                 }
             }
             $model = $this->prompt_until_in($arguments[1], moojon_db::show_tables(), 'What model would you like to generate a scaffold for?');
             $controller = $this->prompt_until($arguments[2], 'Please enter a controller name', $model);
             moojon_generator::scaffold($app, $model, $controller);
             break;
     }
 }
Example #19
0
function rest_breadcrumb($attributes = array())
{
    $attributes = try_set_attribute($attributes, 'class', 'breadcrumb');
    $segments = explode('/', moojon_uri::get_match_pattern());
    if ($segments[0] == APP) {
        array_shift($segments);
    }
    $count = count($segments);
    $ul = ul_tag();
    $href = '/';
    for ($i = 0; $i < $count; $i++) {
        $segment = $segments[$i];
        $attributes = $i == $count - 1 ? array('class' => 'last') : array();
        $link = true;
        if (moojon_base::is_symbol($segment)) {
            $symbol_name = moojon_base::get_symbol_name($segment);
            $href .= moojon_request::get($symbol_name) . '/';
            if ($symbol_name != moojon_primary_key::NAME) {
                if (moojon_paths::get_model_path($segment)) {
                    $content = model_from_symbol($segment);
                }
            } else {
                $content = model_from_id($segments[$i - 1]);
            }
        } else {
            if ($i == $count - 1) {
                $link = true;
            }
            $content = title_text($segment);
            $href .= $segment . '/';
        }
        if ($content) {
            if ($i == $count - 1) {
                $ul->add_child(li_tag($content, $attributes));
            } else {
                $ul->add_child(li_tag(a_tag($content, $href), $attributes));
            }
        }
    }
    return div_tag($ul, $attributes);
}
 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;
 }