Skip to content

Commit

Permalink
更新了很多东西
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggngyv committed Oct 23, 2019
1 parent dcbb0f7 commit 68f6fbd
Show file tree
Hide file tree
Showing 14 changed files with 625 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js linguist-language=php
*.css linguist-language=php
*.html linguist-language=php
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
"schnittstabil/csrf-tokenservice": "^3.1",
"lootils/uuid": "^1.0",
"overclokk/cookie": "^1.0",
"phpfastcache/phpfastcache": "^7.1",
"fzaninotto/faker": "^1.6",
"creitive/breadcrumbs": "^3.1",
"voku/anti-xss": "^2.0",
"gregwar/captcha": "1.*",
"katzgrau/klogger": "^1.2",
"doctrine/annotations": "dev-master",
"doctrine/annotations": "^1.8",
"overtrue/pinyin": "~3.0"
},
"extra": {
Expand Down
25 changes: 20 additions & 5 deletions core/includes/common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response\RedirectResponse;
use Hunter\Core\FormApi\Layui;
use Hunter\Core\FormApi\Bootstrap;
use Hunter\Core\FormApi\Seven;
use voku\helper\AntiXSS;
use Overclokk\Cookie\Cookie;
use Gregwar\Image\Image;
Expand Down Expand Up @@ -241,10 +242,9 @@ function hunter_convert_to_utf8($data) {
* 转换文件名.
*/
function hunter_rename($text, $ext = false, $type = 'date', $format = 'YmdHis') {
$string = new StringConverter();
$houzhui = substr(strrchr($text, '.'), 1);
$basename = basename($text,".".$houzhui);
$newname = $string->createMachineName($basename);
$newname = machine_name($basename);
if($type == 'date'){
$newname .= '_'.date($format, time());
}
Expand Down Expand Up @@ -298,7 +298,7 @@ function hunter_map_assoc($array, $function = NULL) {
*/
function hunter_form_render($type, &$fields, $module = NULL, $parms = NULL, $wrap = TRUE, $parentform = NULL) {
global $app;
$form_id = 'exampleForm-'.rand(1,100);
$form_id = 'hpForm-'.rand(1,100);
if(isset($fields['form_id'])){
$form_id = $fields['form_id'];
unset($fields['form_id']);
Expand Down Expand Up @@ -329,9 +329,11 @@ function hunter_form_render($type, &$fields, $module = NULL, $parms = NULL, $wra
}

if($wrap){
if($type == 'layui'){
if($type == 'layui') {
$form = Layui::create()->start($form_redirect, $form_id, $class, $show_type, $enctype);
}else{
}elseif($type == 'seven') {
$form = Seven::create()->start($form_redirect, $form_id, $class, $show_type, $enctype);
}else {
$form = Bootstrap::create()->start($form_redirect, $form_id, $class, $show_type, $enctype);
}
}else {
Expand Down Expand Up @@ -428,6 +430,10 @@ function hunter_form_render($type, &$fields, $module = NULL, $parms = NULL, $wra
if(isset($field['#disabled']) && $field['#disabled']){
$field['#attributes']['disabled'] = 'true';
}
if(isset($field['#multiple']) && $field['#multiple']){
$field['#attributes']['multiple'] = 'multiple';
$field['#name'] = $name.'[]';
}
element_set_attributes($field, array('id', 'name', 'size'));
$form->select($name, $field);
break;
Expand Down Expand Up @@ -641,6 +647,15 @@ function module_exists($module) {
return $app->getModuleHandle()->moduleExists($module);
}

/**
* create machine name.
*/
function machine_name($text) {
$string = new StringConverter();
$machine_name = $string->createMachineName($text);
return $machine_name;
}

if (!function_exists('e')) {
/**
* Escape HTML entities in a string.
Expand Down
10 changes: 10 additions & 0 deletions core/includes/theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ function theme($target = 'default') {
* @param $parameters 变量
*/
function view($name, array $parameters = array(), $target = 'default') {
global $default_admin_theme, $default_front_theme;

if($default_admin_theme !== 'admin' && strpos($name, 'admin') !==false) {
$name = str_replace("admin", $default_admin_theme, $name);
}

if($default_front_theme !== 'front' && strpos($name, 'front') !==false) {
$name = str_replace("front", $default_front_theme, $name);
}

return theme($target)->setTemplate($name)->setParameters($parameters)->render($name, $parameters);
}

Expand Down
15 changes: 15 additions & 0 deletions core/lib/Hunter/App/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,21 @@ protected function buildRouters($container) {
}
}

if(module_exists('views')) {
$all_views = views_get_all();
static $view_paths = array();
foreach ($all_views as $view) {
if(isset($view['view_path']) && $view['view_path'] && !isset($this->routeTitles[$view['view_path']]) && !isset($view_paths[$view['view_path']])){
$view_paths[$view['view_path']] = $view['view_name'];
$route = $routers->map(['GET','POST'], $view['view_path'], '\Hunter\views\Controller\ViewsUIController::api_get_view');
}

if(is_object($route) && isset($view['view_permissions']) && !empty($view['view_permissions'])){
$this->routePermission[$view['view_path']] = $view['view_permissions'];
}
}
}

$this->routers = $routers;
$this->container->add('routePermission', $this->routePermission);
$this->container->add('routeOptions', $this->routeOptions);
Expand Down
59 changes: 32 additions & 27 deletions core/lib/Hunter/App/Strategy/HunterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,49 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\RedirectResponse;
use Hunter\Core\Utility\StringConverter;
use Hunter\Core\Cache\FastCache;

class HunterStrategy extends ApplicationStrategy implements StrategyInterface {
/**
* {@inheritdoc}
*/
public function getCallable(Route $route, array $vars) {
return function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use ($route, $vars) {
global $default_theme, $hunter_static, $app;
$generate_html = false;
global $default_theme, $hunter_static, $app, $hunter_debug, $static_time;
timer_start();
$path = $route->getPath();
$routeNames = $route->getContainer()->get('routeNames');
$routeOptions = $route->getContainer()->get('routeOptions');
//if enabled html static, and file exists, then load it
$routePermission = $route->getContainer()->get('routePermission');

if($hunter_static && isset($routeNames[$path]) && !isset($routeOptions[$path]['no_cache']) && substr($path, 0, 6) != '/admin' && substr($path, 0, 5) != '/api/') {
$generate_file = 'sites/html/'.$default_theme.'/'.str_replace('.', '/', $routeNames[$path]);
if($vars){
$generate_file .= '_'.implode('_',array_values($vars));
}
if(is_file($generate_file.'.html')){
require_once($generate_file.'.html');
die;
}else {
$generate_html = true;
}
}
$FastCache = FastCache::getInstance();
$string = new StringConverter();
$serverParams = $request->getServerParams();
$key = $string->createMachineName(ltrim($serverParams['REQUEST_URI'], '/'))."_cache";

if(isset($routeOptions[$path]['init'])){
foreach ($app->getModuleHandle()->getImplementations('init') as $module) {
$app->getModuleHandle()->invoke($module, 'init', array($request));
}
}
if (!$FastCache->isCached($key)) {
$routeTitles = $route->getContainer()->get('routeTitles');

$routeTitles = $route->getContainer()->get('routeTitles');
if(isset($routeTitles[$path]) && function_exists('theme')){
theme()->getEnvironment()->addGlobal('page_title', $routeTitles[$path]);
}
$vars['vars'] = $vars;
$body = $route->getContainer()->call($route->getCallable(), $vars);

if(isset($routeTitles[$path]) && function_exists('theme')){
theme()->getEnvironment()->addGlobal('page_title', $routeTitles[$path]);
}
$vars['vars'] = $vars;
$body = $route->getContainer()->call($route->getCallable(), $vars);
$FastCache->set($key, $body, $static_time);
} else {
$body = $FastCache->get($key);
}
}else {
$routeTitles = $route->getContainer()->get('routeTitles');

if($generate_html){
$this->htmlMake($body, $generate_file);
if(isset($routeTitles[$path]) && function_exists('theme')){
theme()->getEnvironment()->addGlobal('page_title', $routeTitles[$path]);
}
$vars['vars'] = $vars;
$body = $route->getContainer()->call($route->getCallable(), $vars);
}

if(is_array($body) || (is_object($body) && get_class($body) == 'stdClass')){
Expand All @@ -63,6 +64,10 @@ public function getCallable(Route $route, array $vars) {
}

if(is_string($body) || is_bool($body)){
if($hunter_debug && (!isset($routePermission[$path]) || isset($routePermission[$path]) && $routePermission[$path] != 'pjax') && !isset($routeOptions[$path]['no_cache']) && substr($path, 0, 6) != '/admin' && substr($path, 0, 5) != '/api/') {
$body .= '<center>页面加载时间: '.timer_read(). ' ms</center>';
}

if ($response->getBody()->isWritable()) {
$response->getBody()->write($body);
}
Expand Down
52 changes: 52 additions & 0 deletions core/lib/Hunter/Cache/FastCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @file
*
* FastCache
*/
namespace Hunter\Core\Cache;

use Phpfastcache\CacheManager;
use Phpfastcache\Config\Config;

class FastCache {

private static $instance = null;
private $conn;

private function __construct(){
$cache_dir = $GLOBALS['cache_dir'];
$this->conn = CacheManager::getInstance('files', new Config(["path" => $cache_dir, "itemDetailedDate" => false]));
}

public static function getInstance(){
if(!self::$instance){
self::$instance = new FastCache();
}
return self::$instance;
}

public function getConnection(){
return $this->conn;
}

public function isCached($key){
$CachedString = $this->conn->getItem($key);
return $CachedString->isHit();
}

public function set($key, $data, $expire = 60){
$CachedString = $this->conn->getItem($key);
$CachedString->set($data)->expiresAfter($expire);
$this->conn->save($CachedString);
}

public function get($key){
return $this->conn->getItem($key)->get();
}

private function __clone(){
}

}
36 changes: 34 additions & 2 deletions core/lib/Hunter/Database/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,37 @@ public function addField($table, $field, $spec) {
if (is_string($spec)) {
$this->connection->query($spec);
}
//todo spec is array

$fixnull = FALSE;
if (!empty($spec['not null']) && !isset($spec['default']) && !$is_primary_key) {
$fixnull = TRUE;
$spec['not null'] = FALSE;
}
$query = 'ALTER TABLE {' . $table . '} ADD ';
$query .= $this->createFieldSql($field, $this->processField($spec));
$this->connection->query($query);
if (isset($spec['initial_from_field'])) {
if (isset($spec['initial'])) {
$expression = 'COALESCE(' . $spec['initial_from_field'] . ', :default_initial_value)';
$arguments = [':default_initial_value' => $spec['initial']];
}
else {
$expression = $spec['initial_from_field'];
$arguments = [];
}
$this->connection->update($table)
->expression($field, $expression, $arguments)
->execute();
}
elseif (isset($spec['initial'])) {
$this->connection->update($table)
->fields([$field => $spec['initial']])
->execute();
}
if ($fixnull) {
$spec['not null'] = TRUE;
$this->changeField($table, $field, $field, $spec);
}
}

/**
Expand All @@ -212,7 +242,9 @@ public function changeField($table, $field, $field_new, $spec) {
if (is_string($spec)) {
$this->connection->query($spec);
}
//todo spec is array

$sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec));
$this->connection->query($sql);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/lib/Hunter/Discovery/PluginDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ReflectionClass;
use Hunter\Core\Utility\Unicode;
use Doctrine\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationReader;

/**
* Provides discovery for plugin within a given set of directories.
Expand Down
Loading

0 comments on commit 68f6fbd

Please sign in to comment.