Last active
March 6, 2018 05:26
-
-
Save picasso250/f4b5a342540cc1a7f62d62653802d057 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Lib; | |
use Exception; | |
use Psr\Container\ContainerInterface; | |
use Psr\Container\ContainerExceptionInterface; | |
use Psr\Container\NotFoundExceptionInterface; | |
class Container implements ContainerInterface | |
{ | |
private static $lazy = []; | |
private static $pool = []; | |
public static function getInstance() { | |
return new self(); | |
} | |
/** | |
* Finds an entry of the container by its identifier and returns it. | |
* | |
* @param string $id Identifier of the entry to look for. | |
* | |
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. | |
* @throws ContainerExceptionInterface Error while retrieving the entry. | |
* | |
* @return mixed Entry. | |
*/ | |
public function get($id) | |
{ | |
if (isset(self::$pool[$id])) return self::$pool[$id]; | |
if (isset(self::$lazy[$id])) { | |
$func = self::$lazy[$id]; | |
$res = $func(); | |
if ($res === null) throw new InitFailException("$id init fail"); | |
return self::$pool[$id] = $res; | |
} | |
throw new NotFoundExceptionInterface("$id not found"); | |
} | |
/** | |
* Returns true if the container can return an entry for the given identifier. | |
* Returns false otherwise. | |
* | |
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. | |
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | |
* | |
* @param string $id Identifier of the entry to look for. | |
* | |
* @return bool | |
*/ | |
public function has($id) | |
{ | |
if (isset(self::$pool[$id])) return true; | |
if (isset(self::$lazy[$id])) { | |
return true; | |
} | |
return false; | |
} | |
public function set($id, $v) | |
{ | |
if (is_callable($v)) self::setLazy($id, $v); | |
else self::setImmediate($id, $v); | |
} | |
public function setLazy($id, $v) { | |
self::$lazy[$id] = $v; | |
} | |
public function setImmediate($id, $v) { | |
self::$pool[$id] = $v; | |
} | |
} | |
class InitFailException extends Exception implements ContainerExceptionInterface | |
{ | |
} | |
class NotFoundException extends Exception implements NotFoundExceptionInterface | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 一些小函数,入选标准:不能只是语法糖,必须做了一些实事 | |
function env_load($dir='.') | |
{ | |
$file = "$dir/.env"; | |
if (!is_file($file)) { | |
echo "no .env file $file\n"; | |
exit(1); | |
} | |
if (!is_readable($file)) { | |
echo ".env $file file not readable\n"; | |
exit(1); | |
} | |
$config = parse_ini_file($file); | |
foreach ($config as $key => $value) { | |
if (!isset($_ENV[$key])) { | |
$_ENV[$key] = $value; | |
} | |
} | |
} | |
function autoload_dir($namespace_root, $dir_root) { | |
spl_autoload_register(function ($nc) use ($namespace_root, $dir_root) { | |
$prefix = "$namespace_root\\"; | |
if (strpos($nc, $prefix) === 0) { | |
$c = substr($nc, strlen($prefix)); | |
require "$dir_root/$c.php"; | |
} | |
}); | |
} | |
function exception_error_handler($severity, $message, $file, $line) { | |
if (!(error_reporting() & $severity)) { | |
// This error code is not included in error_reporting | |
return; | |
} | |
throw new ErrorException($message, 0, $severity, $file, $line); | |
} | |
function error2exception() | |
{ | |
set_error_handler("exception_error_handler"); | |
} | |
function make_csrf($token_name = '_csrf_token_name') | |
{ | |
//After the user's login has deemed to be successful. | |
//Generate a secure token using openssl_random_pseudo_bytes. | |
$myToken = bin2hex(openssl_random_pseudo_bytes(24)); | |
//Store the token as a session variable. | |
$_SESSION[$token_name] = $myToken; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Lib; | |
/** | |
* | |
*/ | |
class RegexRouter | |
{ | |
private static $_rule = []; | |
public static function add($reg, callable $func, $method = "GET") | |
{ | |
self::$_rule[$reg][$method] = $func; | |
} | |
public static function run() | |
{ | |
$url = self::get_url(); | |
foreach (self::$_rule as $reg => $arr) { | |
foreach ($arr as $method => $func) { | |
if ($_SERVER["REQUEST_METHOD"] == $method && preg_match($reg, $url, $m)) { | |
$func($m); | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
private static function get_url() { | |
$uri = $_SERVER["REQUEST_URI"]; | |
$a = explode("?", $uri); | |
return $a[0]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Lib; | |
class Request { | |
public static function GET($name, $default = "") { | |
if (isset($_GET[$name])) { | |
return trim($_GET[$name]); | |
} | |
return $default; | |
} | |
public static function POST($name, $default = "") { | |
if (isset($_POST[$name])) { | |
return trim($_POST[$name]); | |
} | |
return $default; | |
} | |
public static function isAjax() { | |
return isset($_GET['ajax']) | |
|| (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) | |
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace lib; | |
class Response { | |
public static function echoJsonOk($data = "", $msg = "OK") { | |
self::echoJson([ | |
"code" => 0, | |
"data" => $data, | |
"msg" => $msg | |
]); | |
} | |
public static function echoJsonError($code, $msg, $data = "") { | |
self::echoJson([ | |
"code" => $code, | |
"data" => $data, | |
"msg" => $msg | |
]); | |
} | |
public static function echoJson(array $json) { | |
header("Content-Type: application/json"); | |
if (isset($_GET['json_pretty_format'])) { | |
echo json_encode($json); | |
} else { | |
echo json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); | |
} | |
} | |
public static function redirect($url="?") | |
{ | |
header("Location: $url"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment