├── Lean
├── Http
│ ├── Response.php
│ └── Request.php
├── File
│ ├── Handler.php
│ ├── Remove.php
│ ├── Upload.php
│ ├── Image.php
│ ├── Download.php
│ └── Export.php
├── Request
│ ├── Get.php
│ ├── Post.php
│ ├── Files.php
│ └── Http.php
├── Lean.php
├── Utils
│ ├── Label.php
│ ├── Hash.php
│ ├── Select.php
│ └── Link.php
├── App2.php
├── Format.php
├── Request.php
├── Null.php
├── Request2.php
├── Auth.php
├── Format
│ ├── Money.php
│ ├── Time.php
│ └── Date.php
├── Mvc.php
├── App.php
├── Session
│ └── Namespace.php
├── Singleton.php
├── Logger.php
├── Session.php
├── Route.php
├── Config.php
├── Route2.php
├── View.php
└── Launch.php
├── composer.json
└── README.md
/Lean/Http/Response.php:
--------------------------------------------------------------------------------
1 | run();
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/Lean/Utils/Label.php:
--------------------------------------------------------------------------------
1 | $string";
8 | }
9 |
10 | static public function error($string)
11 | {
12 | if (!empty($string)) return $out = "$string";
13 | }
14 | }
--------------------------------------------------------------------------------
/Lean/App2.php:
--------------------------------------------------------------------------------
1 | $value) {
11 | $this->{$key} = $value;
12 | }
13 | }
14 |
15 | public function decode($to = 'ISO-8859-1', $from = 'UTF-8')
16 | {
17 | foreach ($this as $key => $value)
18 | {
19 | $this->$key = mb_convert_encoding($value, $to, $from);
20 | }
21 |
22 | return $this;
23 | }
24 | }
--------------------------------------------------------------------------------
/Lean/Format.php:
--------------------------------------------------------------------------------
1 | =5.3.0"
17 | },
18 | "autoload": {
19 | "psr-4": {
20 | "Lean\\": "Lean/",
21 | "app\\": "../../../app/"
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/Lean/Request2.php:
--------------------------------------------------------------------------------
1 | setAttribute('AUTH_SESSION', 'YES');
14 |
15 | return true;
16 | }
17 |
18 | return false;
19 | }
20 |
21 | static public function isAuth()
22 | {
23 | if (self::$auth == false) return true;
24 |
25 | if (Myapp_Session::session()->getAttribute('AUTH_SESSION') === 'YES')
26 | {
27 | return true;
28 | }
29 |
30 | return false;
31 | }
32 |
33 | static public function discart()
34 | {
35 | Myapp_Session::session()->unsetAttribute('AUTH_SESSION');
36 | //Myapp_Session::session('AUTH_SESSION', 'NO');
37 | return true;
38 | }
39 | }
--------------------------------------------------------------------------------
/Lean/Format/Money.php:
--------------------------------------------------------------------------------
1 |
16 | *
17 | * $_REQUEST[action] = 'test';
18 | * $this->test_action();
19 | *
20 | * $_POST[action] = 'hello';
21 | * $this->hello_action();
22 | *
23 | *
24 | */
25 | public function __construct()
26 | {
27 | if(!empty($this->request()->action))
28 | {
29 | $method_action = $this->request()->action . '_action';
30 |
31 | if(method_exists($this, $method_action))
32 | {
33 | $this->{$method_action}();
34 | }
35 | }
36 | }
37 |
38 |
39 | public function view()
40 | {
41 | return View::singleton(get_class($this));
42 | }
43 | }
--------------------------------------------------------------------------------
/Lean/File/Export.php:
--------------------------------------------------------------------------------
1 |
22 | * Hash::generate(8, Hash::ALPHA);
23 | * Hash::generate(5, Hash::ALPHA_ONLY_UPPER);
24 | * Hash::generate(8, Hash::ALPHA_ONLY_LOWER . Hash::NUMERIC);
25 | * Hash::generate(10, Hash::SYMBOLS);
26 | * Hash::generate(10, Hash::ALL);
27 | *
28 | *
29 | * @param integer $sizeof Tamanho do hash a ser criado
30 | * @param string $caracters
31 | * @return hash
32 | */
33 | static public function generate($sizeof = 8, $caracters = self::NUMERIC)
34 | {
35 | $hash = NULL;
36 |
37 | for ($i = 0; $i < $sizeof; $i++) {
38 | $hash .= substr($caracters, rand(0, strlen($caracters)-1), 1);
39 | }
40 |
41 | return $hash;
42 | }
43 | }
--------------------------------------------------------------------------------
/Lean/Utils/Select.php:
--------------------------------------------------------------------------------
1 | $contentdefault";
33 | }
34 |
35 | if ($options)
36 | {
37 | foreach ($options as $key => $option)
38 | {
39 | if ($value === (string) $key)
40 | $output .= "";
41 | else
42 | $output .= "";
43 | }
44 | }
45 |
46 | return $output;
47 | }
48 | }
--------------------------------------------------------------------------------
/Lean/App.php:
--------------------------------------------------------------------------------
1 |
10 | *
11 | * $_REQUEST[action] = 'test';
12 | * $this->test_action();
13 | *
14 | * $_POST[action] = 'hello';
15 | * $this->hello_action();
16 | *
17 | *
18 | */
19 | public function __construct()
20 | {
21 | if(!empty($this->request()->action))
22 | {
23 | $method_action = $this->request()->action . '_action';
24 |
25 | if(method_exists($this, $method_action))
26 | {
27 | $this->{$method_action}();
28 | }
29 | }
30 | }
31 |
32 | /**
33 | * @return Format
34 | */
35 | public function format()
36 | {
37 | return Format::singleton();
38 | }
39 |
40 | /**
41 | * @return Logger
42 | */
43 | public function logger()
44 | {
45 | return Logger::singleton();
46 | }
47 |
48 | /**
49 | * @return Http\Request
50 | */
51 | public function request()
52 | {
53 | return Http\Request::singleton();
54 | }
55 |
56 | /**
57 | * @return View
58 | */
59 | public function view()
60 | {
61 | return View::singleton(get_class($this));
62 | }
63 | }
--------------------------------------------------------------------------------
/Lean/Session/Namespace.php:
--------------------------------------------------------------------------------
1 | namespace = 'NAMESPACE_'.$namespace;
12 |
13 | parent::__construct();
14 | }
15 |
16 | static public function session($namespace)
17 | {
18 | return new Myapp_Session_Namespace($namespace);
19 | }
20 |
21 | public function getAttribute($key = null)
22 | {
23 | if ($key)
24 | {
25 | $ns = parent::getAttribute($this->namespace);
26 |
27 | if ($ns === null) return null;
28 |
29 | return $ns[$key];
30 | }
31 | else
32 | {
33 | return self::session($this->namespace);
34 | }
35 | }
36 |
37 | public function setAttribute($key, $value)
38 | {
39 | parent::setAttribute($this->namespace, array($key => $value));
40 | }
41 |
42 | public function unsetAttribute($key)
43 | {
44 | parent::setAttribute($this->namespace, array($key => null));
45 | }
46 |
47 | public function isNamespace()
48 | {
49 | parent::isSession($this->namespace);
50 | }
51 |
52 | public function clear()
53 | {
54 | parent::unsetAttribute($this->namespace);
55 | }
56 |
57 | public function destroy()
58 | {
59 | $this->clear();
60 | }
61 | }
--------------------------------------------------------------------------------
/Lean/Utils/Link.php:
--------------------------------------------------------------------------------
1 | ";
12 | }
13 |
14 | static public function addStyle($path, $media = 'all')
15 | {
16 | self::$styles[] = "";
17 | }
18 |
19 | static public function loadScripts()
20 | {
21 | echo implode('', self::$scripts);
22 | }
23 |
24 | static public function loadStyles()
25 | {
26 | echo implode('', self::$styles);
27 | }
28 |
29 | static public function image($path, $alt = null, $title = null, $class= null)
30 | {
31 | echo "
";
32 | }
33 |
34 | static public function loadJQuery($version = '1.7', $env = null)
35 | {
36 | if(APPLICATION_ENV == 'development' || $env == 'development')
37 | {
38 | $output = "";
39 | }
40 | else
41 | {
42 | $output = "";
43 | }
44 |
45 | print $output;
46 | }
47 | }
--------------------------------------------------------------------------------
/Lean/Singleton.php:
--------------------------------------------------------------------------------
1 | $var();
62 | }
63 |
64 | //public function __call($method, $args) { return Null::singleton(); }
65 | public function __call($method, $args) { return null; }
66 |
67 | }
68 | ?>
--------------------------------------------------------------------------------
/Lean/Logger.php:
--------------------------------------------------------------------------------
1 | getMessage();
18 | $code = $exception->getCode();
19 | $file = $exception->getFile();
20 | $line = $exception->getLine();
21 | $trace = $exception->getTraceAsString();
22 | $date = date('M d, Y h:iA');
23 |
24 | $log_message = "
Exception information:
25 |
26 | Date: {$date}
27 |
28 |
29 |
30 | Message: {$message}
31 |
32 |
33 |
34 | Code: {$code}
35 |
36 |
37 |
38 | File: {$file}
39 |
40 |
41 |
42 | Line: {$line}
43 |
44 |
45 | Stack trace:
46 | {$trace}
47 |
48 | Request information:
49 | ";
50 | foreach ($_REQUEST as $key => $value) {
51 | $log_message .= "$key: $value
";
52 | }
53 | $log_message .= "";
54 |
55 | $log_message .= "Server information:
56 | ";
57 | foreach ($_SERVER as $key => $value) {
58 | $log_message .= "$key: $value
";
59 | }
60 | $log_message .= "
61 |
62 |
";
63 |
64 | if( is_file($error_file) === false ) {
65 | file_put_contents($error_file, '');
66 | }
67 |
68 | if( $clear ) {
69 | $content = '';
70 | } else {
71 | $content = file_get_contents($error_file);
72 | }
73 |
74 | file_put_contents($error_file, $log_message . $content);
75 | }
76 | }
--------------------------------------------------------------------------------
/Lean/Session.php:
--------------------------------------------------------------------------------
1 | startSession();
17 |
18 | self::$instance->setNamesession($namesession);
19 |
20 | return self::$instance;
21 | }
22 |
23 | private function __construct() { }
24 |
25 | public function __destruct()
26 | {
27 | $this->closeSession();
28 | }
29 |
30 | private function startSession()
31 | {
32 | if (!isset($_SESSION))
33 | {
34 | session_start();
35 | }
36 | }
37 |
38 | public function closeSession()
39 | {
40 | if (!isset($_SESSION))
41 | {
42 | session_write_close();
43 | }
44 | }
45 |
46 | public function clear()
47 | {
48 | $this->destroy();
49 | }
50 |
51 | public function destroy()
52 | {
53 | unset($_SESSION[$this->namesession]);
54 | return true;
55 | }
56 |
57 | public function destroyAll()
58 | {
59 | if (isset($_SESSION)) session_destroy();
60 | return true;
61 | }
62 |
63 | public function getAttr($key = NULL)
64 | {
65 | if ($key === null)
66 | {
67 | return $_SESSION[$this->namesession];
68 | }
69 | else
70 | {
71 | if($this->isSession($key))
72 | {
73 | return $_SESSION[$this->namesession][$key];
74 | }
75 | else
76 | {
77 | return null;
78 | }
79 | }
80 | }
81 |
82 | public function isSession($key)
83 | {
84 | if(isset($_SESSION[$this->namesession][$key])) {
85 | return true;
86 | }
87 | else {
88 | return false;
89 | }
90 | }
91 |
92 | public function setAttr($key, $value)
93 | {
94 | $_SESSION[$this->namesession][$key] = $value;
95 | }
96 |
97 | public function unsetAttr($key)
98 | {
99 | unset($_SESSION[$this->namesession][$key]);
100 | }
101 |
102 | public function getNamesession()
103 | {
104 | return $this->namesession;
105 | }
106 |
107 | public function setNamesession($namesession)
108 | {
109 | $this->namesession = $namesession;
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/Lean/Route.php:
--------------------------------------------------------------------------------
1 | routes[ Request::ANY ][ $uri ] = $action;
29 | }
30 |
31 | public function any($uri, $action)
32 | {
33 | $this->routes[ Request::ANY ][ $uri ] = $action;
34 | }
35 |
36 | public function get($uri, $action)
37 | {
38 | $this->routes[ Request::GET ][ $uri ] = $action;
39 | }
40 |
41 | public function delete($uri, $action)
42 | {
43 | $this->routes[ Request::DELETE ][ $uri ] = $action;
44 | }
45 |
46 | public function post($uri, $action)
47 | {
48 | $this->routes[ Request::POST ][ $uri ] = $action;
49 | }
50 |
51 | public function put($uri, $action)
52 | {
53 | $this->routes[ Request::PUT ][ $uri ] = $action;
54 | }
55 |
56 |
57 |
58 | /**
59 | * @example {
60 | * Route::alias('home', 'index');
61 | * Route::alias(array('home', 'main'), 'index');
62 | * }
63 | *
64 | * @param (uri or array) $from
65 | * @param uri $to
66 | * @throws RouteException
67 | */
68 | public function alias($from, $to)
69 | {
70 | if(!isset(self::$routes[$to])) throw new RouteException("route $to indefined");
71 |
72 | if (is_array($from)) {
73 |
74 | foreach ($from as $f) {
75 |
76 | self::set($f, self::$routes[$to]);
77 |
78 | }
79 |
80 | } else {
81 |
82 | self::set($from, self::$routes[$to]);
83 |
84 | }
85 | }
86 |
87 | public static function set_routes_path($path)
88 | {
89 | self::$routes_path = $path;
90 |
91 | include_once $path;
92 | }
93 |
94 | public static function get_routes_path()
95 | {
96 | return self::$routes_path;
97 | }
98 |
99 | public static function get_routes()
100 | {
101 | return self::$routes;
102 | }
103 |
104 | public static function set_route_controller_default($controller)
105 | {
106 | self::$route_controller_default = $controller;
107 | }
108 |
109 | public static function get_route_controller_default()
110 | {
111 | return self::$route_controller_default;
112 | }
113 |
114 | public static function set_route_module_default($module)
115 | {
116 | self::$route_module_default = $module;
117 | }
118 |
119 | public static function get_route_module_default()
120 | {
121 | return self::$route_module_default;
122 | }
123 |
124 | public static function set_route_default($route)
125 | {
126 | self::$route_default = $route;
127 | }
128 |
129 | public static function get_route_default()
130 | {
131 | return self::$route_default;
132 | }
133 |
134 | }
135 |
136 | class RouteException extends \Exception { }
--------------------------------------------------------------------------------
/Lean/View.php:
--------------------------------------------------------------------------------
1 | view['directory'] = $this->view['class'] = strtolower(str_replace('Controller', '', $classname));
35 |
36 | $namespace = explode('\\', $namespace);
37 |
38 | $this->view['app'] = $namespace[0];
39 |
40 | $this->view['module'] = $namespace[1];
41 | }
42 |
43 | $this->lean = $this->app = App::singleton();
44 | }
45 |
46 | public function renderPath($pathfile) {
47 | include_once $pathfile;
48 | }
49 |
50 | public function render($options = 'index') {
51 | /**
52 | * render content page
53 | */
54 | $this->temp = array();
55 | $this->set_options_views('temp', $options);
56 | $this->make('temp');
57 |
58 | return $this;
59 | }
60 |
61 | public function set($name, $options = 'index')
62 | {
63 | $this->$name = array();
64 | $this->set_options_views($name, $options);
65 | return $this;
66 | }
67 |
68 | private function set_options_views($var, $options)
69 | {
70 | $this->{$var}['app'] = $this->view['app'];
71 | $this->{$var}['module'] = $this->view['module'];
72 | $this->{$var}['directory'] = $this->view['directory'];
73 | $this->{$var}['extension'] = self::$extension_default;
74 |
75 | if(is_string($options))
76 | {
77 | $options = array_reverse(explode('.', $options));
78 |
79 | $pos = array('page', 'directory', 'module', 'app');
80 |
81 | foreach ($options as $key => $option)
82 | {
83 | $this->{$var}[$pos[$key]] = $option;
84 | }
85 | }
86 | elseif(is_array($options))
87 | {
88 | if (isset($options['page'])) {
89 | $this->{$var}['page'] = $options['page'];
90 | }
91 |
92 | if (isset($options['app'])) {
93 | $this->{$var}['app'] = $options['app'];
94 | }
95 |
96 | if (isset($options['module'])) {
97 | $this->{$var}['module'] = $options['module'];
98 | }
99 |
100 | if (isset($options['directory'])) {
101 | $this->{$var}['directory'] = $options['directory'];
102 | }
103 |
104 | if (isset($options['extension'])) {
105 | $this->{$var}['extension'] = $options['extension'];
106 | }
107 | }
108 | }
109 |
110 | public function make($name) {
111 | include_once $this->{$name}['app'] . DIRECTORY_SEPARATOR . $this->{$name}['module'] . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $this->{$name}['directory'] . DIRECTORY_SEPARATOR . $this->{$name}['page'] . $this->{$name}['extension'];
112 | }
113 |
114 | public function redirect($url) {
115 | header('location:' . $url); die();
116 | }
117 |
118 | public function get_property($name = null)
119 | {
120 | if(array_key_exists($name, $this->view))
121 | {
122 | return $this->view[$name];
123 | }
124 | else
125 | {
126 | return null;
127 | }
128 | }
129 |
130 | public function __get($var) { return Null::singleton(); }
131 | }
--------------------------------------------------------------------------------
/Lean/Launch.php:
--------------------------------------------------------------------------------
1 | process();
20 | }
21 |
22 | private function dispache($module, $controller, $method, array $params = null)
23 | {
24 | if(empty($module)) {
25 | $module = Route::get_route_module_default();
26 | }
27 |
28 | if(empty($controller)) {
29 | $controller = Route::get_route_controller_default();
30 | }
31 |
32 | if(empty($method)) {
33 | $method = 'index';
34 | }
35 |
36 | $class = Config::get_application_path() . "\\" . Config::get_module_prefixe() . $module . "\\controllers\\" . $controller . "Controller";
37 |
38 | if ($params) {
39 | $num_param = 0;
40 | $set_params = rtrim(str_repeat('$params[$num_param++],', count($params)), ',');
41 | }
42 | else {
43 | $set_params = '';
44 | }
45 |
46 | eval($class.'::singleton()->'.$method.'('.$set_params.');');
47 | }
48 |
49 | private function get_info()
50 | {
51 | /* verifica url com index.php */
52 | preg_match('/(.*)\/index.php(.*)/', $_SERVER["PHP_SELF"], $matches);
53 |
54 | /* relative uri base */
55 | $relative_uri_base = $matches[1];
56 |
57 | /* route path information */
58 | $path_info = $matches[2];
59 |
60 | $url = Request::singleton()->getUrl();
61 |
62 | /* verifica url sem index.php */
63 | if(!preg_match('/index.php/', $url->getPath())) {
64 | $path_info = str_replace($relative_uri_base, '', $url->getPath());
65 | }
66 |
67 | /* define url root */
68 | define(strtoupper(Config::get_root_path()), $relative_uri_base.'/');
69 |
70 | $path_info = trim($path_info, '/');
71 |
72 | return $path_info;
73 | }
74 |
75 | private function process()
76 | {
77 |
78 | $info = self::get_info();
79 |
80 | /*
81 | * rotas
82 | */
83 | if($routes = Route::get_routes())
84 | {
85 | if(array_key_exists($info, $routes))
86 | {
87 | $route = $routes[$info];
88 |
89 | if(is_object($route))
90 | {
91 | $route();
92 | }
93 | elseif(is_array($route))
94 | {
95 | $module = isset($route['module']) ? $route['module'] : NULL;
96 | $controller = isset($route['controller']) ? $route['controller'] : NULL;
97 | $method = isset($route['method']) ? $route['method'] : NULL;
98 | $params = isset($route['params']) ? (array) $route['params'] : NULL;
99 |
100 | $this->dispache($module, $controller, $method, $params);
101 |
102 | }
103 |
104 | return true;
105 | }
106 | }
107 |
108 |
109 | /*
110 | * rota padrão caso info seja vazio
111 | */
112 | if(empty($info))
113 | {
114 | $route_default = Route::get_route_default();
115 | if(!empty($route_default)) $info = $route_default;
116 | }
117 |
118 |
119 | /*
120 | * explode info
121 | */
122 | if(isset($info))
123 | {
124 | $url_string = explode('/', strtolower(trim($info, '/')));
125 | if($url_string[0] == '') $url_string = null;
126 | }
127 |
128 |
129 | /*
130 | * valida e dispara método
131 | */
132 | $module = isset($url_string[0]) ? $url_string[0] : null;
133 |
134 | $controller = isset($url_string[1]) ? (str_replace(' ', '', ucwords(str_replace('-', ' ', $url_string[1])))) : null;
135 |
136 | $method = isset($url_string[2]) ? str_replace('-', '_', $url_string[2]) : null;
137 |
138 | $this->dispache($module, $controller, $method);
139 |
140 | }
141 |
142 |
143 | }
--------------------------------------------------------------------------------
/Lean/Format/Time.php:
--------------------------------------------------------------------------------
1 | 59) $m = '59';
46 | if ($s > 59) $s = '59';
47 |
48 | if ($format == self::FORMAT_HOUR_MINUTES)
49 | return "$h:$m";
50 |
51 | if ($format == self::FORMAT_HOUR_MINUTES_SECONDS)
52 | return "$h:$m:$s";
53 |
54 | return strftime($format, strtotime($hour));
55 | }
56 |
57 | /**
58 | * Retorna hora atual no formato HH:MM:SS
59 | * @return string Hora no formato HH:MM:SS
60 | */
61 | // public static function now() {
62 | // return date('H:i:s');
63 | // }
64 |
65 | public static function now($format = self::FORMAT_HOUR_MINUTES_SECONDS) {
66 | return strftime($format);
67 | }
68 |
69 | public static function time_to_seconds($time) {
70 | $hours = substr($time, 0, -6);
71 | $minutes = substr($time, -5, 2);
72 | $seconds = substr($time, -2);
73 |
74 | return $hours * 3600 + $minutes * 60 + $seconds;
75 | }
76 |
77 | public static function seconds_to_time($seconds) {
78 | $hours = floor($seconds / 3600);
79 | $minutes = floor($seconds % 3600 / 60);
80 | $seconds = $seconds % 60;
81 |
82 | return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
83 | }
84 |
85 | public static function sum($time1, $time2, $op = 'add')
86 | {
87 | $now = new \DateTime(Date::now(Date::FORMAT_DATE));
88 | if ($op != 'add') $op == 'sub';
89 |
90 | $time1_slices = explode(':', $time1);
91 | $time1_hour = isset($time1_slices[0]) ? (int) $time1_slices[0] : '00';
92 | $time1_min = isset($time1_slices[1]) ? (int) $time1_slices[1] : '00';
93 | $time1_sec = isset($time1_slices[2]) ? (int) $time1_slices[2] : '00';
94 |
95 | $time2_slices = explode(':', $time2);
96 | $time2_hour = isset($time2_slices[0]) ? (int) $time2_slices[0] : '00';
97 | $time2_min = isset($time2_slices[1]) ? (int) $time2_slices[1] : '00';
98 | $time2_sec = isset($time2_slices[2]) ? (int) $time2_slices[2] : '00';
99 |
100 | $now->add(new \DateInterval("PT{$time1_hour}H{$time1_min}M{$time1_sec}S"));
101 | $now->{$op}(new \DateInterval("PT{$time2_hour}H{$time2_min}M{$time2_sec}S"));
102 | $time = $now->format('H:i:s');
103 |
104 | return $time;
105 |
106 | }
107 |
108 | public static function subtract($time1, $time2) {
109 | return self::sum($time1, $time2, 'sub');
110 | }
111 | }
--------------------------------------------------------------------------------
/Lean/Format/Date.php:
--------------------------------------------------------------------------------
1 | $value) {
11 | $this->{$key} = $value;
12 | }
13 | }
14 |
15 | /**
16 | * @return self
17 | */
18 | public static function singleton() {
19 | return parent::singleton();
20 | }
21 |
22 | public function decode($to = 'ISO-8859-1', $from = 'UTF-8') {
23 | foreach ($this as $key => $value) {
24 | $this->$key = mb_convert_encoding($value, $to, $from);
25 | }
26 |
27 | return $this;
28 | }
29 |
30 | /**
31 | * @return Request
32 | */
33 | public function delete() {
34 | return Post::singleton();
35 | }
36 |
37 | /**
38 | * @return Request
39 | */
40 | public function files() {
41 | return Files::singleton();
42 | }
43 |
44 | /**
45 | * @return Request
46 | */
47 | public function get() {
48 | return Get::singleton();
49 | }
50 |
51 | /**
52 | * @return Request
53 | */
54 | public function head() {
55 | return Post::singleton();
56 | }
57 |
58 | /**
59 | * @return Request
60 | */
61 | public function post() {
62 | return Post::singleton();
63 | }
64 |
65 | /**
66 | * @return Request
67 | */
68 | public function put() {
69 | return Post::singleton();
70 | }
71 |
72 | /**
73 | * @return Method
74 | */
75 | public function getMethod() {
76 | return Method::singleton();
77 | }
78 |
79 | /**
80 | * @return Url
81 | */
82 | public function getUrl() {
83 | return Url::singleton();
84 | }
85 | }
86 |
87 | /**
88 | * Class Post
89 | * @package Lean\Http
90 | */
91 | class Post extends Request
92 | {
93 | public function __construct() {
94 | parent::__construct($_POST);
95 | }
96 | }
97 |
98 | /**
99 | * Class Files
100 | * @package Lean\Http
101 | */
102 | class Files extends Request
103 | {
104 | public function __construct() {
105 | parent::__construct($_FILES);
106 | }
107 | }
108 |
109 | /**
110 | * Class Get
111 | * @package Lean\Http
112 | */
113 | class Get extends Request
114 | {
115 | public function __construct() {
116 | parent::__construct($_GET);
117 | }
118 | }
119 |
120 | /**
121 | * Class Url
122 | * @package Lean\Http
123 | */
124 | class Url extends Singleton {
125 |
126 | private $uri_parsed;
127 |
128 | /**
129 | * @return self
130 | */
131 | public static function singleton() {
132 | return parent::singleton();
133 | }
134 |
135 | public function __construct() {
136 | $this->uri_parsed = parse_url($this->getUri());
137 | }
138 |
139 | public function getHash() {
140 | return isset($this->uri_parsed['fragment']) ? $this->uri_parsed['fragment'] : null;
141 | }
142 |
143 | public function getHost() {
144 | return $_SERVER['SERVER_NAME'];
145 | }
146 |
147 | public function getHostWithPort() {
148 | return $this->getHost() . ':' . $this->getPort();
149 | }
150 |
151 | public function getPath() {
152 | return isset($this->uri_parsed['path']) ? $this->uri_parsed['path'] : null;
153 | }
154 |
155 | public function getPort() {
156 | return $_SERVER['SERVER_PORT'];
157 | }
158 |
159 | public function getProtocol() {
160 | return isset($this->uri_parsed['scheme']) ? $this->uri_parsed['scheme'] : null;
161 | }
162 |
163 | public function getQuery() {
164 | return isset($this->uri_parsed['query']) ? $this->uri_parsed['query'] : null;
165 | }
166 |
167 | public function getUri() {
168 | $url = strtok($_SERVER['REQUEST_URI'], '?');
169 |
170 | if (empty($url)) {
171 | return $_SERVER['REQUEST_URI'];
172 | } else {
173 | return $url .'?'. urlencode($_SERVER['QUERY_STRING']);
174 | }
175 | }
176 |
177 | public function getUrl() {
178 | return $this->getHost() . $this->getUri();
179 | }
180 |
181 | public function __toString() {
182 | return $this->getUrl();
183 | }
184 | }
185 |
186 |
187 | /**
188 | * Class Method
189 | * @package Lean\Http
190 | */
191 | class Method extends Singleton {
192 |
193 | const METHOD_DELETE = 'DELETE';
194 | const METHOD_GET = 'GET';
195 | const METHOD_HEAD = 'HEAD';
196 | const METHOD_OPTIONS = 'OPTIONS';
197 | const METHOD_PATCH = 'PATCH';
198 | const METHOD_POST = 'POST';
199 | const METHOD_PUT = 'PUT';
200 |
201 | /**
202 | * @return self
203 | */
204 | public static function singleton() {
205 | return parent::singleton();
206 | }
207 |
208 | public function getMethod() {
209 | return $_SERVER['REQUEST_METHOD'];
210 | }
211 |
212 | public function isDelete() {
213 | return $this->getMethod() == self::METHOD_DELETE;
214 | }
215 |
216 | public function isGet() {
217 | return $this->getMethod() == self::METHOD_GET;
218 | }
219 |
220 | public function isHead() {
221 | return $this->getMethod() == self::METHOD_HEAD;
222 | }
223 |
224 | public function isOptions() {
225 | return $this->getMethod() == self::METHOD_OPTIONS;
226 | }
227 |
228 | public function isPatch() {
229 | return $this->getMethod() == self::METHOD_PATCH;
230 | }
231 |
232 | public function isPost() {
233 | return $this->getMethod() == self::METHOD_POST;
234 | }
235 |
236 | public function isPut() {
237 | return $this->getMethod() == self::METHOD_PUT;
238 | }
239 |
240 | public function __toString() {
241 | return $this->getMethod();
242 | }
243 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Lean PHP Framework
2 |
3 | Lean PHP Framework is a micro framework PHP (~40KB). Modern frameworks are powerfull but so much complicated,
4 | the mostly of resources you never gonna use, some functionality sounds good but if you don't really need it's a
5 | only waste of time.
6 | With Lean you can construct fast e lightweight softwares, with follows resources:
7 |
8 | * Structure MVC, REST or both;
9 | * Requests;
10 | * Routes (automatic or custom);
11 | * Namespaces;
12 | * Class autoload;
13 | * PHP code hidden;
14 | * Basic template engine;
15 | * Date and Time manipulation;
16 | * Easy configuration;
17 |
18 | ### Requirement
19 |
20 | PHP 5.3+
21 |
22 | ### Basic structure
23 |
24 | ```php
25 | -- rootdir
26 | -- app
27 | -- main (module)
28 | -- controllers
29 | -- HomeController.php
30 | -- models
31 | -- views
32 | -- home
33 | -- index.phtml
34 | -- Bootstrap.php
35 | -- Routes.php
36 | -- public_html
37 | -- css
38 | -- js
39 | -- img
40 | -- index.php
41 | -- .htaccess
42 | -- vendor
43 | -- composer
44 | -- lean
45 | -- autoload.php
46 | ```
47 |
48 | Create into your rootdir teh follows directories:
49 |
50 | - `app`: You will write all your application php into app directory (controllers, models, views and configs), this way your application not stay exposed.
51 |
52 | - `public_html`: Into public_html directory we have only **index.php** as file .php. You can put all yours public files, like css, javascripts, images, fonts, etc.
53 |
54 | - `vendor`: Composer will create it and copy our lib to lean directory.
55 |
56 | ## Getting started
57 |
58 | ### Instalation
59 |
60 | Install via [Composer](http://getcomposer.org "Composer")
61 |
62 | ```bash
63 | composer require lean/lean
64 | ```
65 |
66 | ### Easy configuration
67 |
68 | create file `index.php` into **public_html** directory
69 |
70 | > Into index.php we have only one line, all of rest application php keep safe into app directory.
71 |
72 | ```php
73 |
74 | ```
75 |
76 | create file `.htaccess` into **public_html** directory to custom urls works
77 |
78 | > Don't forget enable mod_rewrite on apache
79 |
80 | ```bash
81 | RewriteEngine On
82 | RewriteCond %{REQUEST_FILENAME} -s [OR]
83 | RewriteCond %{REQUEST_FILENAME} -l [OR]
84 | RewriteCond %{REQUEST_FILENAME} -d
85 | RewriteRule ^.*$ - [NC,L]
86 | RewriteRule ^.*$ index.php [NC,L]
87 | ```
88 |
89 | Create file `Bootstrap.php` into **app** directory
90 |
91 | ```php
92 | run();
118 | ```
119 |
120 | Well done! It's all configuration necessary to run like a pro.
121 |
122 |
123 | ### Hello world
124 |
125 | Regardless of whether their application is rest or not, I think is a good ideia keep your access logic always into controllers,
126 | into Routes.php you keep only routes ;)
127 |
128 | ```php
129 | Remember, in your site type only **www.your-domain.com**, everything else php is hidden.
144 |
145 |
146 | ## Automatic route controller
147 |
148 | http://localhost/rootdir/public_html/`$1`/`$2`/`$3`
149 |
150 | * `$1` : Module - if not informed, use main module (main directory)
151 | * `$2` : Controller - if not informed, instance indexController class
152 | * `$3` : Method - if not informed, call index method
153 |
154 | ```php
155 | uri `/main/product` result is **About Product!**
177 | > uri `/main/product/index` result is **About Product!**
178 | > uri `/main/product/features-list` result is **Product list!**
179 | > uri `/main/product/features_list` result is **Product list!**
180 | > uri `/main/product/buy` result is **Processing your order...**
181 | > uri `/main/product/buy-action` result is **Processing your order...**
182 | > uri `/main/product/buy_action` result is **Processing your order...**
183 |
184 | To IndexController example, the result is:
185 |
186 | > uri `/` result is **Hello World!**
187 | > uri `/main` result is **Hello World!**
188 | > uri `/main/index` result is **Hello World!**
189 | > uri `/main/index/index` result is **Hello World!**
190 |
191 |
192 | ## Custom routes
193 |
194 | ### Config routes file
195 |
196 | In `app/Bootstrap.php` add file routes before launch Lean
197 |
198 | ```php
199 | ...
200 |
201 | /**
202 | * routes file
203 | */
204 | Lean\Route::set_routes_path('app/Routes.php');
205 |
206 | /**
207 | * init lean framework
208 | */
209 | Lean\Launch::instance()->run();
210 | ```
211 |
212 | ### Basic route
213 |
214 | create file `Routes.php` into **app** directory
215 |
216 | ```php
217 | Url: http://your-site.com/foo/bar // result is 'Hi'
226 |
227 | ### Route to method in controller
228 |
229 | ```php
230 | 'product',
235 | ));
236 |
237 | Route::set('resources', array(
238 | 'controller' => 'product',
239 | 'method' => 'resources_list'
240 | ));
241 |
242 | Route::set('learn-more-about-product', array(
243 | 'controller' => 'product',
244 | 'method' => 'resources_list'
245 | ));
246 | ```
247 |
248 | > Url `http://your-site.com/product` result is **About Product**
249 | > Url `http://your-site.com/resources` result is **Product List**
250 | > Url `http://your-site.com/learn-more-about-product` result is **Product List**
251 |
252 | ### Route to different module
253 |
254 | ```php
255 | 'api'
260 | 'controller' => 'payment',
261 | ));
262 | ```
263 |
264 | ### Simple route alias
265 |
266 | ```php
267 | Route::alias('old-page-about-product', 'product');
268 | ```
269 |
270 | ### Multiple route alias
271 |
272 | ```php
273 | Route::alias(array('old-page-about-product', 'foo', 'bar'), 'product');
274 | ```
275 |
276 | ## Request object
277 |
278 | Recovery request data in controllers
279 |
280 | ```php
281 | request->name;
294 | echo $this->request->last_name;
295 |
296 | /**
297 | * get only method post - same of variable $_POST
298 | */
299 | echo $this->request()->post()->name;
300 |
301 | /**
302 | * get only method post - same of variable $_POST
303 | */
304 | echo $this->request()->get()->name;
305 |
306 | /**
307 | * get only method file - same of variable $_FILE
308 | */
309 | $request = $this->request()->file()->name;
310 |
311 | /**
312 | * you can too instance request object
313 | */
314 | $request = new \Lean\Http\Request();
315 | $request->name
316 |
317 | ...
318 | }
319 | }
320 | ```
321 |
322 | ## Using Views
323 |
324 | In views directory, you must create `product` and `layout` subdirectories with `.phtml` files.
325 |
326 | ```php
327 | ...
328 | -- controllers
329 | -- ProductController.php
330 | -- models
331 | -- views
332 | -- product
333 | -- index.phtml
334 | -- edit.phtml
335 | -- layout
336 | -- header.phtml
337 | -- footer.phtml
338 | -- template.html
339 | ...
340 | ```
341 |
342 | Create `template.phtml` in layout directory, you can include header and footer parts here
343 |
344 | ```html
345 |
346 |
347 | My new app
348 |
349 |
350 |
351 |
352 | $this->app->view->render('layout.header') ?>
353 |
354 |
355 |
356 | $this->app->view->make('content') ?>
357 |
358 |
359 |