a tiny mvc framework for php using php-activerecord
1<?php
2/*
3 main entry point
4*/
5
6if (floatval(phpversion()) < 5.3)
7 die("PHP version of at least 5.3 is required and you are using "
8 . phpversion() . ")");
9
10/* where our app lives (not just where halfmoon lives) */
11define("HALFMOON_ROOT", realpath(__DIR__ . "/../../"));
12
13/* some sane defaults */
14date_default_timezone_set("UTC");
15session_name("_halfmoon_session");
16
17if (!defined("HALFMOON_ENV")) {
18 if (getenv("HALFMOON_ENV"))
19 define("HALFMOON_ENV", getenv("HALFMOON_ENV"));
20 else
21 /* assume to be in the development environment unless told otherwise */
22 define("HALFMOON_ENV", "development");
23}
24
25require_once(HALFMOON_ROOT . "/halfmoon/lib/singleton.php");
26require_once(HALFMOON_ROOT . "/halfmoon/lib/config.php");
27require_once(HALFMOON_ROOT . "/halfmoon/lib/exceptions.php");
28require_once(HALFMOON_ROOT . "/halfmoon/lib/logging.php");
29require_once(HALFMOON_ROOT . "/halfmoon/lib/utilities.php");
30require_once(HALFMOON_ROOT . "/halfmoon/lib/rescuer.php");
31
32require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/global_helper.php");
33require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/helper.php");
34require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/html_helper.php");
35require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/form_common.php");
36require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/form_tag_helper.php");
37require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/form_helper.php");
38require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/time_helper.php");
39require_once(HALFMOON_ROOT . "/halfmoon/lib/helpers/prototype_helper.php");
40
41require_once(HALFMOON_ROOT . "/halfmoon/lib/controller.php");
42require_once(HALFMOON_ROOT . "/halfmoon/lib/request.php");
43require_once(HALFMOON_ROOT . "/halfmoon/lib/router.php");
44
45if (file_exists(HALFMOON_ROOT . "/config/db.ini"))
46 require_once(HALFMOON_ROOT . "/halfmoon/lib/php-activerecord/"
47 . "ActiveRecord.php");
48
49/* append our root to the include path to pick up user-installed code */
50set_include_path(get_include_path() . PATH_SEPARATOR . HALFMOON_ROOT);
51
52/* load site-specific boot settings */
53if (file_exists(HALFMOON_ROOT . "/config/boot.php"))
54 require_once(HALFMOON_ROOT . "/config/boot.php");
55
56/* autoload controllers and helpers as needed */
57function halfmoon_autoload($class_name) {
58 if (preg_match("/^([A-Za-z0-9_]+)(Controller|Helper)$/", $class_name, $m)) {
59 $file = HALFMOON_ROOT . "/";
60
61 /* Controller -> controllers/ */
62 $file .= strtolower($m[2]) . "s/";
63
64 /* CamelCase -> camel_case_controller.php */
65 $words = preg_split('/(?<=\\w)(?=[A-Z])/', $m[1]);
66 $file .= strtolower(join("_", $words)) . "_" . strtolower($m[2])
67 . ".php";
68
69 if (file_exists($file))
70 require_once($file);
71 }
72}
73
74spl_autoload_register("halfmoon_autoload", false, false);
75
76if (defined("PHP_ACTIVERECORD_ROOT"))
77 HalfMoon\Config::initialize_activerecord();
78
79/* bring in any post-framework but pre-route code */
80if (file_exists($f = HALFMOON_ROOT . "/config/application.php"))
81 require_once($f);
82
83/* bring in the route table and route our request */
84if (file_exists(HALFMOON_ROOT . "/config/routes.php"))
85 HalfMoon\Router::initialize(function($router) {
86 require_once(HALFMOON_ROOT . "/config/routes.php");
87 });
88
89?>