Skip to content

Commit abf522b

Browse files
authored
SunPHP
Efficient PHP framework. 1.The entrance control 2.The development of modular 3.Template control 4.Coupling separation
1 parent ca13676 commit abf522b

14 files changed

Lines changed: 466 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/* This is the user test file.
3+
* @author sun
4+
*/
5+
namespace Applications\Controls;
6+
use Sunphp\Lib\Controls\TemplateControl;
7+
class IndexControl extends TemplateControl{
8+
/**
9+
* The default controller callback method.
10+
*/
11+
public function indexAction(){
12+
//The template below shows the default folder
13+
$this->display("index.html");
14+
}
15+
16+
public function guideAction(){
17+
//The following template according to specified folder
18+
$this->display("common.html","include");
19+
}
20+
21+
/**
22+
* Test the custom callback method.
23+
*/
24+
public function testAction(){
25+
var_dump("IndexControl testAction");
26+
}
27+
28+
29+
}
30+
31+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
OK
9+
<br/>
10+
This is a SunPHP demo(Display the specified).
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
OK
9+
<br/>
10+
This is a SunPHP demo(optional)
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
OK
9+
<br/>
10+
This is a SunPHP demo(A single template)
11+
</body>
12+
</html>

Sunphp/Autoloader.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Autoload.
4+
* @author sun
5+
* @date: 2018/1/12 14:43
6+
*/
7+
namespace Sunphp;
8+
use Sunphp\Lib\Exceptions\FileException;
9+
use Sunphp\Lib\Runtimes\Data;
10+
11+
/**
12+
* Autoload.
13+
*/
14+
class Autoloader
15+
{
16+
/**
17+
* Autoload root path.
18+
*
19+
* @var string
20+
*/
21+
protected static $_autoloadRootPath = '';
22+
23+
/**
24+
* Set autoload root path.
25+
*
26+
* @param string $root_path
27+
* @return void
28+
*/
29+
public static function setRootPath($root_path)
30+
{
31+
self::$_autoloadRootPath = $root_path;
32+
}
33+
34+
/**
35+
* Load files by namespace.
36+
*
37+
* @param string $name
38+
* @return boolean
39+
*/
40+
public static function loadByNamespace($name)
41+
{
42+
$class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
43+
44+
if (self::$_autoloadRootPath) {
45+
$class_file = self::$_autoloadRootPath . DIRECTORY_SEPARATOR . $class_path . '.php';
46+
}
47+
48+
if (empty($class_file) || !is_file($class_file)) {
49+
$class_file = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . "$class_path.php";
50+
}
51+
52+
try
53+
{
54+
if (is_file($class_file)) {
55+
/** @noinspection PhpIncludeInspection */
56+
require_once($class_file);
57+
if (class_exists($name, false)) {
58+
return true;
59+
}
60+
}else{
61+
throw new FileException( $class_file." File doesn't exist");
62+
}
63+
}
64+
catch(FileException $e)
65+
{
66+
Data::$exception_list[] = $e->getException();
67+
}
68+
return false;
69+
}
70+
}
71+
72+
spl_autoload_register('Sunphp\Autoloader::loadByNamespace');

Sunphp/Lib/Commons/Common.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
*
4+
* @author: sun<798049214@qq.com>
5+
* @date: 2018/1/12 23:33
6+
*/
7+
8+
namespace Sunphp\Lib\Commons;
9+
10+
11+
class Common
12+
{
13+
14+
}

Sunphp/Lib/Configs/Config.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Sunup\Lib\Configs\config sit;
4+
* @author: sun<798049214@qq.com>
5+
* @date: 2018/1/13 13:31
6+
*/
7+
8+
namespace Sunphp\Lib\Configs;
9+
10+
11+
class Config
12+
{
13+
const VERSION = '1.0.5';
14+
const CONTROL_ADDRESS = 'Applications\\Controls\\';
15+
const TEMPLATE_ADDRESS = 'Applications\\Templates\\';
16+
public static $template = null;
17+
18+
}

Sunphp/Lib/Controls/Control.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* This is a controller class.
4+
* @author sun
5+
* @date: 2018/1/11 18:43
6+
*/
7+
8+
namespace Sunphp\Lib\Controls;
9+
use Sunphp\Lib\Configs\Config;
10+
use Sunphp\Lib\Exceptions\ControlException;
11+
use Sunphp\Lib\Runtimes\Data;
12+
13+
/**
14+
* This is a controller class.
15+
*/
16+
class Control{
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $controlName = "c";
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $controlDefault = "Index";
27+
/**
28+
* @var string
29+
*/
30+
protected $controlPostfix = "Control";
31+
/**
32+
* @var string
33+
*/
34+
protected $functionName = "a";
35+
/**
36+
* @var string
37+
*/
38+
protected $functionDefault = "index";
39+
/**
40+
* @var string
41+
*/
42+
protected $functionPostfix = "Action";
43+
44+
/**
45+
*
46+
* @param null
47+
*/
48+
public function __construct()
49+
{
50+
51+
}
52+
53+
public function start()
54+
{
55+
$url_c = isset($_GET[$this->controlName]) ? $_GET[$this->controlName] : $this->controlDefault;
56+
$url_control = $url_c . $this->controlPostfix;
57+
58+
$url_a = isset($_GET[$this->functionName]) ? $_GET[$this->functionName] : $this->functionDefault;
59+
$url_action = $url_a . $this->functionPostfix;
60+
61+
$class_name = Config::CONTROL_ADDRESS . $url_control;
62+
$fn_name = $url_action;
63+
$params = array();
64+
try
65+
{
66+
if (class_exists($class_name)) {
67+
if (method_exists(new $class_name, $fn_name)) {
68+
call_user_func_array(array(new $class_name, "$fn_name") , $params);
69+
} else {
70+
throw new ControlException($class_name . "->" . $fn_name. " function doesn't exist");
71+
//If the exception is thrown, this text will not be shown
72+
}
73+
} else {
74+
throw new ControlException($class_name . " class doesn't exist");
75+
}
76+
}
77+
catch(ControlException $e)
78+
{
79+
Data::$exception_list[] = $e->getException();
80+
}
81+
82+
}
83+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* This is a class that controls the template.
4+
* @author: sun<798049214@qq.com>
5+
* @date: 2018/1/12 18:40
6+
*/
7+
8+
namespace Sunphp\Lib\Controls;
9+
use Sunphp\Lib\Configs\Config;
10+
use Sunphp\Lib\Exceptions\ControlException;
11+
use Sunphp\Lib\Runtimes\Data;
12+
13+
class TemplateControl
14+
{
15+
16+
17+
/**
18+
* @param $name
19+
* @param string $folder
20+
* @return bool
21+
*/
22+
public function display($name,$folder = '')
23+
{
24+
if ('' != Config::$template) {
25+
$template_folder = Config::$template . DIRECTORY_SEPARATOR;
26+
} else {
27+
$template_folder = '';
28+
}
29+
$control_template = str_replace(array(Config::CONTROL_ADDRESS,'Control'),"", get_class($this));
30+
$dir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR ;
31+
if ('' == $folder) {
32+
$template_file = $dir . Config::TEMPLATE_ADDRESS .$template_folder. $control_template . DIRECTORY_SEPARATOR . $name;
33+
} else {
34+
$template_file = $dir . Config::TEMPLATE_ADDRESS .$template_folder. $folder . DIRECTORY_SEPARATOR . $name;
35+
}
36+
$template_file = str_replace('\\', DIRECTORY_SEPARATOR, $template_file);
37+
try
38+
{
39+
if (is_file($template_file)) {
40+
require_once($template_file);
41+
return true;
42+
} else {
43+
throw new ControlException($template_file . " doesn't exist");
44+
//If the exception is thrown, this text will not be shown
45+
46+
}
47+
}
48+
catch(ControlException $e)
49+
{
50+
Data::$exception_list[] = $e->getException();
51+
}
52+
return false;
53+
}
54+
55+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Control exception.
4+
* @author: sun<798049214@qq.com>
5+
* @date: 2018/1/13 14:25
6+
*/
7+
8+
namespace Sunphp\Lib\Exceptions;
9+
use Exception;
10+
11+
class ControlException extends Exception
12+
{
13+
public function getException($type = 1)
14+
{
15+
$err = [
16+
'type' => "control_exception",
17+
'msg' => $this->getMessage(),
18+
'file' => $this->getFile(),
19+
'line' => $this->getLine()
20+
];
21+
if ($type == 1) {
22+
return json_encode($err);
23+
}
24+
return $err;
25+
}
26+
}

0 commit comments

Comments
 (0)