1
<?php
2
/*
3
  Belokan Micro Framework
4
  Licensed under GNU GPL
5
  http://gitorious.org/belokan
6
  ---
7
  Belokan/Belokan.php:
8
  Belokan core class
9
*/
10
11
class Belokan {
12
  
13
  private
14
    $_config;
15
  
16
  public function __construct ($config_file=null)
17
  {
18
    spl_autoload_register('Belokan::autoloader');
19
    $this->_config = Belokan_Config::getInstance();
20
    if ($config_file != null) $this->_config->loadFile($config_file);
21
  }
22
  
23
  public function run ()
24
  {
25
    $logBelokan = $this->_config->get('log', 'belokan');
26
    $log = Belokan_Log::getInstance();
27
    $req = Belokan_Request::getInstance();
28
    $ctrl = $req->getController();
29
    $action = $req->getAction();
30
    $ctrlClass = $ctrl.'Controller';
31
    $actionMethod = $action.'Action';
32
    $RESTCtrl = false;
33
34
    if (!class_exists($ctrlClass)) {
35
      if ($logBelokan)
36
        $log->add('Belokan', 'Request unknown controller ('.$ctrl.'). HTTP 404 error generated.');
37
      $actionMethod = 'e404';
38
      $ctrlInstance = new HttpErrorsController('HttpErrors', 'e404');
39
    } else {
40
      $ctrlInstance = new $ctrlClass($ctrl, $action);
41
      $RESTCtrl = $ctrlInstance instanceof Belokan_REST_Controller;
42
43
      if (!$RESTCtrl && !method_exists($ctrlClass, $actionMethod)) {
44
        if ($logBelokan)
45
          $log->add('Belokan', 'Request unknown action ('.$action.') for controller '.$ctrl.'. HTTP 404 error generated.');
46
        $actionMethod = 'e404';
47
        $ctrlInstance = new HttpErrorsController('HttpErrors', 'e404');
48
      }
49
    }
50
51
    if ($RESTCtrl) $ctrlInstance->setView('default');
52
    if ($ctrlInstance->_pre_action() !== false) {
53
      $actionReturnValue = ($RESTCtrl) ?
54
        call_user_func(array($ctrlInstance, strtolower($req->getMethod())), $action)
55
        : call_user_func(array($ctrlInstance, $actionMethod));
56
      if ($actionReturnValue !== false)
57
        $ctrlInstance->_post_action();
58
    }
59
    $ctrlInstance->_view->renderLayoutBefore($layout = $ctrlInstance->getLayout());
60
    $ctrlInstance->_pre_view();
61
    $ctrlInstance->_view->renderView($ctrlInstance->getView());
62
    $ctrlInstance->_post_view();
63
    $ctrlInstance->_view->renderLayoutAfter($layout);
64
    $ctrlInstance->_post_render();
65
  }
66
  
67
  public static function autoloader ($class_name)
68
  {
69
    if (substr($class_name, 0, 8) == 'Belokan_')
70
      $classPath = dirname(__FILE__).str_replace('_', '/', substr($class_name, 7)).'.php';
71
    else if (substr($class_name, -10) == 'Controller')
72
      $classPath = Belokan_Config::getInstance()->get('dir', 'controllers').substr($class_name, 0, -10).'.php';
73
    else {
74
      $classPath = Belokan_Config::getInstance()->get('dir', 'models').str_replace('_', '/', $class_name).'.php';
75
      if (!file_exists($classPath) && Belokan_Config::getInstance()->exists('dir', 'lib'))
76
        $classPath = Belokan_Config::getInstance()->get('dir', 'lib').str_replace('_', '/', $class_name).'.php';
77
    }
78
  
79
    if (file_exists($classPath)) {
80
      require_once $classPath;
81
      return true;
82
    } else {
83
      if (Belokan_Config::getInstance()->get('log', 'belokan'))
84
        Belokan_Log::getInstance()->add('Belokan', 'ERROR: Trying to load an inexistant class ('.$class_name.').');
85
      return false;
86
    }
87
  }
88
  
89
}