diff --git a/Youngs/Core/Request.php b/Youngs/Core/Request.php new file mode 100644 index 0000000..1df2345 --- /dev/null +++ b/Youngs/Core/Request.php @@ -0,0 +1,125 @@ +_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; + } + +} diff --git a/Youngs/Core/Route.php b/Youngs/Core/Route.php new file mode 100644 index 0000000..61e115e --- /dev/null +++ b/Youngs/Core/Route.php @@ -0,0 +1,157 @@ +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'; + } + } + +} \ No newline at end of file diff --git a/Youngs/Core/YsController.php b/Youngs/Core/YsController.php new file mode 100644 index 0000000..7c38cd9 --- /dev/null +++ b/Youngs/Core/YsController.php @@ -0,0 +1,24 @@ +render($viewName, $data); + } + + +} \ No newline at end of file diff --git a/Youngs/Core/YsModel.php b/Youngs/Core/YsModel.php new file mode 100644 index 0000000..b65af65 --- /dev/null +++ b/Youngs/Core/YsModel.php @@ -0,0 +1,14 @@ +data); + if (is_array($data)) { + extract($data); + } + $filename = SITE_VIEW_PATH . $viewName . '.php'; + if (is_file($filename)) { + include $filename; + } + } + +} \ No newline at end of file diff --git a/Youngs/Core/newEmptyPHP_4.php b/Youngs/Core/newEmptyPHP_4.php new file mode 100644 index 0000000..1df2345 --- /dev/null +++ b/Youngs/Core/newEmptyPHP_4.php @@ -0,0 +1,125 @@ +_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; + } + +} diff --git a/Youngs/Youngs.php b/Youngs/Youngs.php new file mode 100644 index 0000000..5dd0618 --- /dev/null +++ b/Youngs/Youngs.php @@ -0,0 +1,40 @@ +autoLoadFile($config); + } + + /** + * + * @param type $config + */ + abstract public function autoLoadFile($config); + + public function setmodule($name, $class) { + $this->$name = new $class(); + } + + public function setModules($modulesArr) { + foreach ($modulesArr as $moduleName => $className) { + $this->setmodule($moduleName, $className); + } + } + + /** + * 获取所有类文件 + * @param type $path + * @return type + */ + public function get_all_files($path = SITE_CLASS_PATH) { + $list = []; + $path = trim($path, '/'); + $fileArr = glob($path . '/*'); + foreach ($fileArr as $filename) { + if (is_dir($filename)) { + $list = array_merge($list, self::get_all_files($filename)); + } else { + $list[] = $filename; + } + } + return $list; + } + + /** + * + */ + public function loadFiles($fileArr) { + $result = false; + if (is_array($fileArr)) { + $reArr = []; + foreach ($fileArr as $filename) { + if ($this->loadFile($filename) === false) + $reArr[] = $filename; + } + $result = true; + } + $result = $result ? (empty($reArr) ? $result : $reArr) : $result; + return $result; + } + + /** + * + * @param type $filename + * @return boolean + */ + public function loadFile($filename) { + if (is_file($filename)) { + include $filename; + } else { + return false; + } + return true; + } + +} diff --git a/Youngs/YsWeb.php b/Youngs/YsWeb.php new file mode 100644 index 0000000..1fc5631 --- /dev/null +++ b/Youngs/YsWeb.php @@ -0,0 +1,61 @@ +autoLoadFile($config); + } + + /** + * + */ + public function run() { + $modulesArr = array( + 'request' => Core\Request::class, + 'route' => Core\Route::class, + ); + $this->setModules($modulesArr); + Youngs::app()->route->run(); +// var_dump([ +// $_SERVER, //服务器和执行环境信息 +// $_GET, //HTTP GET 变量 +// $_POST, //HTTP POST 变量 +// $_FILES, //HTTP 文件上传变量 +// $_REQUEST, //HTTP Request 变量 +//// $_SESSION, //Session 变量 +// $_ENV, //环境变量 +// $_COOKIE, //HTTP Cookies +//// $php_errormsg, //前一个错误信息 +//// $HTTP_RAW_POST_DATA, //原生POST数据 +//// $http_response_header, //HTTP 响应头 +//// $argc, //传递给脚本的参数数目 +//// $argv, //传递给脚本的参数数组 +// ]); + } + + public function autoLoadFile($config) { + + $coreArr = $this->get_all_files(YS_PATH . 'Core/'); + $this->loadFiles($coreArr); + $controllerArr = $this->get_all_files(SITE_CLASS_PATH . 'Controllers/'); + $this->loadFiles($controllerArr); + + $config[] = ['controllerArr' => $controllerArr]; + $this->config = $config; + } + +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a62cf3f --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "youngs/youngs", + "description": "own frame", + "homepage": "http://github.com/y835246563/youngs", + "license": "MIT", + "authors": [ + { + "name": "y83246563", + "email": "youngshuo@qq.com" + } + ] + +}