Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
y835246563 committed Aug 23, 2018
1 parent eb027f5 commit 001d607
Show file tree
Hide file tree
Showing 10 changed files with 679 additions and 0 deletions.
125 changes: 125 additions & 0 deletions Youngs/Core/Request.php
@@ -0,0 +1,125 @@
<?php
namespace Youngs\Core;

/**
* @Author youngshuo@qq.com
* @Create Time: 2018-08-17 11:01:19
* @Description:
*/
use Youngs\Youngs;

class Request {

private $_requestUri;
private $_urlPath;
private $_pathParams = [];

// private $_pathInfo;
// private $_scriptFile;
// private $_scriptUrl;
// private $_hostInfo;
// private $_baseUrl;
// private $_cookies;
// private $_preferredAcceptTypes;
// private $_preferredLanguages;
// private $_csrfToken;
// private $_restParams;




public function all($name = null) {
switch ($name) {
case 'get':
return $_GET;
break;

case 'post':
return $_POST;
break;

case 'all':
return array_merge($_GET, $_POST, $this->_pathParams);
break;

default:
return array_merge($_GET, $_POST);
break;
}
}

public function get($name, $defaultValue = null) {
return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
}

public function post($name, $defaultValue = null) {
return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
}

public function any($name, $defaultValue = null) {
return isset($this->_pathParams[$name]) ? $this->_pathParams[$name] : (isset($_POST[$name]) ? $_POST[$name] : (isset($_GET[$name]) ? $_GET[$name] : $defaultValue));
// return isset($_POST[$name]) ? $_POST[$name] : (isset($_GET[$name]) ? $_GET[$name] : $defaultValue);
}

public function getPathParam($name, $defaultValue = null) {
return isset($this->_pathParams[$name]) ? $this->_pathParams[$name] : $defaultValue;
}

public function getUri() {
if ($this->_requestUri === null) {
if (isset($_SERVER['REQUEST_URI'])) {
$this->_requestUri = $_SERVER['REQUEST_URI'];
} else {
$this->_requestUri = "can't get REQUEST_URI,\$_SERVER['REQUEST_URI'] is not set,please check this method";
}
}

return $this->_requestUri;
}

public function getBaseUrl() {
// if (isset($_SERVER['HTTP_HOST'])) {
// $this->_baseUrl = '';
// }
// return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

public function getUrlPath() {
if ($this->_urlPath === null) {
$uri = $this->getUri();
$path = explode('?', $uri, 2)[0];
$path = str_replace('\\', '/', $path);
$path = preg_replace('/\/+/', '/', $path);
$this->_urlPath = trim($path, '/');
}
return $this->_urlPath;
}

public function getUrlInfo($types = ['uri', 'baseUrl', 'urlPath']) {
if (is_string($types)) {
$method = 'get' . ucfirst($types);
if (method_exists($this, $method)) {
$urlInfo = $this->$method();
} else {
$urlInfo = "this message can't get, no method to get it ";
}
} elseif (is_array($types)) {
$urlInfo = [];
foreach ($types as $type) {
$method = 'get' . ucfirst($type);
if (method_exists($this, $method)) {
$urlInfo[$type] = $this->$method();
} else {
$urlInfo[$type] = "this message can't get, no method to get it ";
}
}
}
return $urlInfo;
}

public function setPathParams($params) {
if (is_array($params))
$this->_pathParams = $param;
}

}
157 changes: 157 additions & 0 deletions Youngs/Core/Route.php
@@ -0,0 +1,157 @@
<?php

namespace Youngs\Core;

/**
* @Author youngshuo@qq.com
* @Create Time: 2018-08-15 11:21:25
* @Description:
*/
use Youngs\Youngs;

class Route {

private $_currentRoute;
private $_routeConfig;
private $_pathParams;

public function __construct() {

}

/**
*
*/
public function run() {
$routeCfg = $this->getRouteConfig();
$route = $this->getRoute();

if (isset($routeCfg[$route])) {
$classArr = explode('@', $routeCfg[$route], 2);
$className = 'App\Http\Controllers\\' . $classArr[0];
if (!isset($classArr[1])) {
$classArr[1] = DEFAULT_ACTION;
}
$this->callMethod($className, $classArr[1]);
} else {
echo ' there should redirect to 404 page';
}
}

/**
* get the route
* @param type $urlPath
* @return type
*/
public function getRoute($urlPath = null) {
if ($this->_currentRoute === null) {
$routeCfg = $this->getRouteConfig();
if ($urlPath === null) {
$urlPath = Youngs::app()->request->getUrlPath();
}
if (isset($this->_routeConfig[$urlPath])) {
$this->_currentRoute = $urlPath;
} else {
$this->matchRoute($urlPath, $routeCfg);
}
}
return $this->_currentRoute;
}

/**
* match the custom route,only use by Route::getRoute()
* @param type $urlPath
* @param type $routeCfg
*/
protected function matchRoute($urlPath, $routeCfg) {
$filterRouteCfg = array_filter($routeCfg, array($this, 'filterCustomRoute'));
foreach ($filterRouteCfg as $route => $value) {
$pattern = preg_replace_callback('/<([\w]*):?([^>]*)>/', array($this, 'setParamName'), $route);
$pattern = '/' . preg_quote($pattern) . '/';
$path = preg_replace_callback($pattern, array($this, 'setParamValue'), $urlPath);
if ($path === '') {
$this->_currentRoute = $urlPath === '' ? '' : $route;
Youngs::app()->request->setPathParams($this->_currentRoute);
break;
}
}
}

/**
* used to filter custom route
* @param type $route
* @return boolean
*/
protected function filterCustomRoute($route) {
if (strpos($route, '<') !== false) {
return true;
}
}

/**
* set the param's name in the urlpath
* @param type $matches
* @return type
*/
protected function setParamName($matches) {
if ($matches[1] != '') {
$this->_pathParams[$matches[1]] = null;
}
return '(' . $matches[2] . ')';
}

/**
* set the param's value in the urlpath
* @param type $matches
* @return string
*/
protected function setParamValue($matches) {
for ($i = 1; $i < count($matches); $i++) {
$this->_pathParams[array_search(NULL, $this->_pathParams)] = $matches[$i];
}
return '';
}

/**
* call the controller action
* @param type $className
* @param type $methodName
* @param type $params Description
* @return boolean
*/
public function callMethod($className, $methodName = '', $params = []) {
if (empty($className) || empty($methodName)) {
return false;
}
if (class_exists($className)) {
$classObj = new $className();
} else {
echo $className . ' is not exists';
}
if (method_exists($classObj, $methodName)) {
$method = new \ReflectionMethod($className, $methodName);
$parameters = $method->getParameters();
$allParam = Youngs::app()->request->all();
foreach ($parameters as $parameter) {
if (isset($params[$parameter->name]) === false) {
$params[] = isset($allParam[$parameter->name]) ? $allParam[$parameter->name] : null;
}
}
call_user_func_array(array($classObj, $methodName), $params);
}
}

public function getRouteConfig() {
$this->setRouteConfig();
return $this->_routeConfig;
}

public function setRouteConfig($config = null) {
if (empty($this->_routeConfig)) {
$this->_routeConfig = empty($config) ? Youngs::app()->config['routeConfig'] : $config;
} elseif (!empty($config)) {
echo 'routeConfig has been set';
}
}

}
24 changes: 24 additions & 0 deletions Youngs/Core/YsController.php
@@ -0,0 +1,24 @@
<?php

namespace Youngs\Core;

/**
* @Author youngshuo@qq.com
* @Create Time: 2018-08-15 12:33:23
* @Description:
*/
use Youngs\Core\YsView;
use Youngs\Youngs;
class YsController {

public function __construct() {

}

public function view($viewName, $data = null) {
$view = new YsView;
$view->render($viewName, $data);
}


}
14 changes: 14 additions & 0 deletions Youngs/Core/YsModel.php
@@ -0,0 +1,14 @@
<?php
namespace Youngs\Core;
/**
* @Author youngshuo@qq.com
* @Create Time: 2018-08-16 18:06:07
* @Description:
*/
class YsModel {

function __construct() {

}

}
30 changes: 30 additions & 0 deletions Youngs/Core/YsView.php
@@ -0,0 +1,30 @@
@@ -0,0 +1,29 @@
<?php

namespace Youngs\Core;

/**
* @Author youngshuo@qq.com
* @Create Time: 2018-08-16 14:49:35
* @Description:
*/
class YsView {

// public $data = [];

function __construct() {

}

public function render($viewName, $data = []) {
// extract($this->data);
if (is_array($data)) {
extract($data);
}
$filename = SITE_VIEW_PATH . $viewName . '.php';
if (is_file($filename)) {
include $filename;
}
}

}

0 comments on commit 001d607

Please sign in to comment.