diff --git a/.gitignore b/.gitignore index a1fdd76..c324cd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,10 @@ .DS_Store .vscode/ +composer.lock composer.phar /vendor/ -tests/data/ +/src/tests/data/ + + diff --git a/composer.json b/composer.json index b109282..64ec3e6 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,15 @@ }, "autoload": { "psr-4": { - "Fasim\\": "src/Fasim" + "Fasim\\Cache\\": "src/Fasim/Cache", + "Fasim\\Core\\": "src/Fasim/Core", + "Fasim\\Db\\": "src/Fasim/Db", + "Fasim\\Event\\": "src/Fasim/Event", + "Fasim\\Facades\\": "src/Fasim/Facades", + "Fasim\\Library\\": "src/Fasim/Library", + "Fasim\\Log\\": "src/Fasim/Log", + "Fasim\\Session\\": "src/Fasim/Session", + "Fasim\\Support\\": "src/Fasim/Cache" }, "files": [ "src/Fasim/Support/helpers.php" diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 855f4c1..0000000 --- a/composer.lock +++ /dev/null @@ -1,80 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", - "This file is @generated automatically" - ], - "content-hash": "f8daf5c384a847d277acd7fa5c0ec170", - "packages": [ - { - "name": "twig/twig", - "version": "v1.33.2", - "source": { - "type": "git", - "url": "https://github.com/twigphp/Twig.git", - "reference": "dd6ca96227917e1e85b41c7c3cc6507b411e0927" - }, - "dist": { - "type": "zip", - "url": "https://files.phpcomposer.com/files/twigphp/Twig/dd6ca96227917e1e85b41c7c3cc6507b411e0927.zip", - "reference": "dd6ca96227917e1e85b41c7c3cc6507b411e0927", - "shasum": "" - }, - "require": { - "php": ">=5.2.7" - }, - "require-dev": { - "psr/container": "^1.0", - "symfony/debug": "~2.7", - "symfony/phpunit-bridge": "~3.3@dev" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.33-dev" - } - }, - "autoload": { - "psr-0": { - "Twig_": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com", - "homepage": "http://fabien.potencier.org", - "role": "Lead Developer" - }, - { - "name": "Armin Ronacher", - "email": "armin.ronacher@active-4.com", - "role": "Project Founder" - }, - { - "name": "Twig Team", - "homepage": "http://twig.sensiolabs.org/contributors", - "role": "Contributors" - } - ], - "description": "Twig, the flexible, fast, and secure template language for PHP", - "homepage": "http://twig.sensiolabs.org", - "keywords": [ - "templating" - ], - "time": "2017-04-20T17:39:48+00:00" - } - ], - "packages-dev": [], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [] -} diff --git a/src/Fasim/Core/Router.php b/src/Fasim/Core/Router.php index a9b355a..1631a0d 100644 --- a/src/Fasim/Core/Router.php +++ b/src/Fasim/Core/Router.php @@ -48,7 +48,10 @@ public function getMatchAction() { public function init() { //获取配置 - $this->modules = Cfg::get('modules'); + $this->modules = Cfg::get('modules', null); + if (!is_array($this->modules)) { + $this->modules = []; + } $wd = $this->getWebsiteDirectory(); $requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; diff --git a/src/Fasim/Widget/Form.php b/src/Fasim/Widget/Form.php deleted file mode 100644 index 5be47b0..0000000 --- a/src/Fasim/Widget/Form.php +++ /dev/null @@ -1,938 +0,0 @@ -data; - } - if ($value === null ) { - if (is_array($key)) { - $this->data = $key; - } else if (is_string($key)) { - return isset($this->data[$key]) ? $this->data[$key] : null; - } else { - return null; - } - } else { - $this->data[$key] = $value; - } - return $this; - } - - public function action($url) { - $this->action = Form::getUrl($url); - return $this; - } - - public function method($method) { - $this->method = $method; - return $this; - } - - public function handle($callback=null) { - $this->hasError = false; - - foreach ($this->controls as $control) { - if ($control instanceof FormValue) { - $pk = 'n_'.str_replace('.', '_-_', $control->key); - $value = isset($_POST[$pk]) ? $_POST[$pk] : ''; - if (!$control->checkRules($value)) { - $this->hasError = true; - } - $di = strpos($control->key, '.'); - if ($di !== false) { - $keys = explode('.', $control->key); - $values = &$this->data; - for ($i = 0; $i < count($keys); $i++) { - $key = $keys[$i]; - if ($i == count($keys) - 1) { - $values[$key] = $value; - } else { - if (!isset($values[$key])) { - $values[$key] = []; - } - $values = &$values[$key]; - } - } - } else { - $this->data[$control->key] = $value; - } - } - } - if ($callback != null && is_callable($callback)) { - $errors = $callback($this->hasError, $this->data); - if (is_array($errors) && count($errors) > 0) { - $this->hasError = true; - foreach ($errors as $ek => $ev) { - $this->addError($ek, $ev); - } - } - } - return !$this->hasError; - } - - public function isSuccess() { - return !$this->hasError; - } - - public function addError($key, $errorWord) { - foreach ($this->controls as $control) { - if (isset($control->key) && $control->key == $key) { - $control->addCustomError($errorWord); - } - } - } - - protected function getValue($key, $values) { - $di = strpos($key, '.'); - - if ($di !== false) { - $nkey = substr($key, $di + 1); - $key = substr($key, 0, $di); - - if (isset($values[$key])) { - return $this->getValue($nkey, $values[$key]); - } - } - if (isset($values[$key])) { - return $values[$key]; - } - //not found - return null; - } - - public function build() { - $html = "
action}\" method=\"{$this->method}\"> \n"; - - $controls = []; - $hiddens = []; - $buttons = []; - - $keys = []; - foreach ($this->controls as $control) { - if ($control instanceof FormValue) { - $value = $this->getValue($control->key, $this->data); - $control->value = $value === null ? $control->value : $value; - $keys[] = $control->key; - } - if ($control instanceof FormHidden) { - $hiddens[] = $control; - } else if ($control instanceof FormButton) { - $buttons[] = $control; - } else { - $controls[] = $control; - } - } - - $hasReferer = false; - foreach ($hiddens as $control) { - $html .= $control->render(); - if ($control->key == 'referer') { - $hasReferer = true; - } - } - if (!$hasReferer) { - $referer = Request::referer(); - $html .= " \n"; - } - - $html .= "
\n"; - foreach ($controls as $control) { - $html .= $control->render(); - } - $html .= "
\n"; - foreach ($buttons as $control) { - $html .= $control->render(); - } - $html .= "
\n"; - - foreach ($keys as $key) { - if (strpos($key, '.') != false) { - $nkey = str_replace('.', '_-_', $key); - $html = str_replace('_'.$key.'"', '_'.$nkey.'"', $html); - } - - } - return $html; - } - - public static function getUrl($url) { - if (strlen($url) < 4 || substr($url, 0, 4) != 'http') { - $adminUrl = Config::baseUrl(); - if ($url{0} == '/') { - $url = substr($url, 0, 1); - } - $url = $adminUrl.$url; - } - return $url; - } - - public function add($control) { - $this->controls[] = $control; - return $this; - } - - public function get($key) { - foreach ($this->controls as $control) { - if ($control->key == $key) { - return $control; - } - } - return null; - } - - public static function newHidden($key='') { - return new FormHidden($key); - } - - public static function newText($key='') { - return new FormText($key); - } - - public static function newSelect($key='', $options=[]) { - return new FormSelect($key, $options); - } - - public static function newImage($key='') { - return new FormImage($key); - } - - public static function newImages($key='') { - return new FormImages($key); - } - - public static function newFile($key='') { - return new FormFile($key); - } - - public static function newFiles($key='') { - return new FormFiles($key); - } - - public static function newTextarea($key='') { - return new FormTextarea($key); - } - - public static function newRichText($key='') { - return new FormRichText($key); - } - - public static function newButton($name='') { - return new FormButton($name); - } - - public static function newHtml($html='') { - return new FormHtml($html); - } - - public static function newScript($html='') { - return new FormScript($html); - } - -} - -interface FormControl { - function render(); -} - -class FormHtml implements FormControl { - public $html = ''; - - public function __construct($html='') { - $this->html($html); - } - - public function html($html='') { - $this->html = $html; - return $this; - } - - public function render() { - return $this->html; - } -} - -class FormScript extends FormHtml { - public function render() { - $html = ''."\n"; - return $html; - } -} - -abstract class FormValue implements FormControl { - public $label = ''; - public $key = ''; - public $value = ''; - public $readonly = false; - - - public $rules = []; - - public $min = 0; - public $max = 10000; - - public $errorWord = ''; - public $errorType = ''; - - public function __construct($key='') { - $this->key($key); - } - - public function label($label) { - $this->label = $label; - return $this; - } - - public function key($key) { - $this->key = $key; - return $this; - } - - public function value($value) { - $this->value = $value; - return $this; - } - - public function readonly($readonly=true) { - $this->readonly = $readonly; - return $this; - } - - public function notEmpty() { - $this->rules[] = 'not_empty'; - return $this; - } - - public function integerValue() { - $this->rules[] = 'integer'; - return $this; - } - - public function numbericValue() { - $this->rules[] = 'numberic'; - return $this; - } - - public function urlValue() { - $this->rules[] = 'url'; - return $this; - } - - public function emailValue() { - $this->rules[] = 'email'; - return $this; - } - - public function min($min) { - $this->min = $min; - return $this; - } - - public function max($max) { - $this->max = $max; - return $this; - } - - public function addRule($rule) { - $this->rules[] = $rule; - return $this; - } - - public function getError() { - - if ($this->errorType == '') { - return ''; - } - if ($this->errorWord != '') { - return $this->errorWord; - } - switch ($this->errorType) { - case 'min': - return '长度必须大于'.$this->min; - case 'max': - return '长度必须小于'.$this->max; - case 'not_empty': - return '不能为空'; - case 'integer': - return '必须是整数'; - case 'numberic': - return '必须是数字'; - case 'url': - return '必须是网址'; - case 'email': - return '必须是Email'; - } - return '格式错误'; - } - - public function error($errorWord) { - $this->errorWord = $errorWord; - } - - public function addCustomError($errorWord) { - $this->errorType = 'custom'; - $this->errorWord = $errorWord; - } - - public function checkRules($value) { - if (strlen($value.'') < $this->min) { - $this->errorType = 'min'; - return false; - } - if (strlen($value.'') > $this->max) { - $this->errorType = 'max'; - return false; - } - foreach ($this->rules as $rule) { - $result = $this->checkRule($rule, $value); - if (!$result) { - $this->errorType = $rule; - return false; - } - } - $this->errorType = ''; - return true; - } - - public function checkRule($rule, $value) { - if ($rule == 'not_empty') { - if (empty($value)) { - return false; - } - } else if (!empty($value)){ - $p = $rule; - if ($rule == 'integer') { - $p = '/^\d+$/s'; - } else if ($rule == 'numberic') { - $p = '/^\d+\.?\d*$/s'; - } else if ($rule == 'email') { - return filter_var($value, FILTER_VALIDATE_EMAIL); - } else if ($rule == 'url') { - return filter_var($value, FILTER_VALIDATE_URL); - } - if (!preg_match($p, $value)) { - return false; - } - } - return true; - } -} - - -class FormButton implements FormControl { - public $name; - public $link = ''; - public $primary = false; - public function __construct($name='') { - $this->name = $name; - } - - public function name($name) { - $this->name = $name; - return $this; - } - - public function link($url) { - $this->primary = false; - $this->url = $url; - return $this; - } - public function primary() { - $this->primary = true; - return $this; - } - - public function render() { - if ($this->primary) { - return " \n"; - } else if ($this->url != '') { - $url = Form::getUrl($this->url); - return "{$this->name} \n"; - } - } - -} - -class FormHidden extends FormValue { - - public function render() { - return "key}\" value=\"{$this->value}\" /> \n"; - } - -} - -class FormGroup extends FormValue { - - public $remark = ''; - - public function remark($remark) { - $this->remark = $remark; - return $this; - } - - public function render() { - $error = $this->getError(); - $errorClass = $error == '' ? '' : ' error'; - $html = "
\n"; - $html .= " \n"; - $html .= "
\n"; - $html .= $this->renderInput(); - if ($error != '') { - $html .= "{$error} \n"; - } - if ($this->remark) { - $html .= "{$this->remark} \n"; - } - $html .= "
\n"; - $html .= "
\n"; - return $html; - } - - public function renderInput() { - return ''; - } -} - -class FormImages extends FormGroup { - public $maxCount = 6; - public $allowFiles = 'jpg,jpeg,gif,png'; - - public function maxCount($maxCount) { - $this->maxCount = $maxCount; - } - - public function allowFiles($allowFiles) { - $this->allowFiles = $allowFiles; - } - - - public function renderInput() { - $fileId = 'i_'.$this->key; - $html = "key}\" value=\"{$this->value}\" /> \n"; - $fileListId = 'fileList_'.$this->key; - $filePickerId = 'filePicker_'.$this->key; - $images = empty($this->value) ? [] : explode(';', $this->value); - $html .= '
'."\n"; - $html .= '
'."\n"; - foreach ($images as $image) { - $html .= '
'."\n"; - } - $html .= '
'."\n"; - $html .= '

上传图片
'."\n"; - $html .= '
'."\n"; - $html .= << -$('body').ready(function(){ - var maxCount = {$this->maxCount}; - var uploader = WebUploader.create({ - auto: true, - server: '/attachment/upload', - pick: '#{$filePickerId}', - accept: { - title: '上传文件', - extensions: '{$this->allowFiles}', - mimeTypes: 'image/*' - } - }); - function checkCount() { - var items = $('#{$fileListId} .file-item'); - $('#{$filePickerId}').toggle(items.length < maxCount); - } - checkCount(); - function removeItem(btn) { - var item = $(btn).closest('div'); - var index = $('#{$fileListId} .file-item').index(item); - item.remove(); - var value = $('#{$fileId}').val().split(';'); - value.splice(index, 1); - $('#{$fileId}').val(value.join(';')); - checkCount(); - } - $('#{$fileListId} .file-item i').click(function(){ - removeItem(this); - }); - uploader.on('fileQueued', function( file ) { - var li = $( - '
' + - '' + - '' + - '
' - ), - img = li.find('img'); - $('#{$fileListId}').append(li); - checkCount(); - li.find('i').click(function(){ - removeItem(this); - }); - uploader.makeThumb(file, function( error, src ) { - if ( error ) { - img.replaceWith('不能预览'); - return; - } - img.attr( 'src', src ); - }, 160, 160 ); - }); - uploader.on('uploadProgress', function( file, percentage ) { - var li = $( '#'+file.id ), percentDiv = li.find('.progress span'); - if ( !percentDiv.length ) { - percentDiv = $('

').appendTo( li ).find('span'); - } - percentDiv.css('width', percentage * 100 + '%' ); - }); - function showError(fileId, msg) { - console.log(fileId); - var li = $('#'+fileId), errorDiv = li.find('p.error'); - if ( !errorDiv.length ) { - errorDiv = $('

').appendTo( li ); - } - errorDiv.text(msg); - } - uploader.on('uploadSuccess', function( file, response ) { - //$( '#'+file.id ).addClass('upload-state-done'); - if (typeof response == 'object') { - if (response.error == 0) { - var value = $('#{$fileId}').val(); - if (value != '') { - value = value + ';' + response.url; - } else { - value = response.url; - } - $('#{$fileId}').val(value); - } else { - showError(file.id, response.message); - } - } else { - showError(file.id, '上传失败'); - } - }); - uploader.on('uploadError', function( file ) { - showError(file.id, '上传失败'); - }); - uploader.on('uploadComplete', function( file ) { - $( '#'+file.id ).find('.progress').remove(); - }); -}); - -EOT; - return $html; - } -} - -class FormImage extends FormImages { - public function renderInput() { - $this->maxCount(1); - return parent::renderInput(); - } -} - -class FormFiles extends FormGroup { - - public $maxCount = 6; - public $allowFiles = 'doc,docx,xls,xlsx,ppt,pptx,txt,rar,zip,7z,html,htm,mp3,mov,mp4,avi'; - - public function maxCount($maxCount) { - $this->maxCount = $maxCount; - } - - public function allowFiles($allowFiles) { - $this->allowFiles = $allowFiles; - } - - public function renderInput() { - $fileId = 'i_'.$this->key; - $html = "key}\" value=\"{$this->value}\" /> \n"; - $fileListId = 'fileList_'.$this->key; - $filePickerId = 'filePicker_'.$this->key; - $files = empty($this->value) ? [] : explode(';', $this->value); - $html .= '
'."\n"; - $html .= '
'."\n"; - foreach ($files as $file) { - list($key, $name) = explode(',', $file, 2); - $ext = substr(strrchr($name, '.'), 1); - $html .= '
'.$name.'
'."\n"; - } - $html .= '
'."\n"; - $html .= '

上传文件
'."\n"; - $html .= '
'."\n"; - $html .= << -$('body').ready(function(){ - var maxCount = {$this->maxCount}; - var uploader = WebUploader.create({ - auto: true, - server: '/attachment/upload?dir=auto', - pick: '#{$filePickerId}', - accept: { - title: '上传文件', - extensions: '{$this->allowFiles}', - //mimeTypes: 'image/*' - } - }); - function checkCount() { - var items = $('#{$fileListId} .file-item'); - $('#{$filePickerId}').toggle(items.length < maxCount); - } - checkCount(); - function removeItem(btn) { - var item = $(btn).closest('div'); - var index = $('#{$fileListId} .file-item').index(item); - item.remove(); - var value = $('#{$fileId}').val().split(';'); - value.splice(index, 1); - $('#{$fileId}').val(value.join(';')); - checkCount(); - } - $('#{$fileListId} .file-item i').click(function(){ - removeItem(this); - }); - uploader.on('fileQueued', function( file ) { - var li = $( - '
' + - '' + - '' + file.name + '' + - '' + - '
' - ), - img = li.find('img'); - $('#{$fileListId}').append(li); - checkCount(); - li.find('i').click(function(){ - removeItem(this); - }); - }); - uploader.on('uploadProgress', function( file, percentage ) { - var li = $( '#'+file.id ), percentDiv = li.find('.progress span'); - if ( !percentDiv.length ) { - percentDiv = $('

').appendTo( li ).find('span'); - } - percentDiv.css('width', percentage * 100 + '%' ); - }); - function showError(fileId, msg) { - console.log(fileId); - var li = $('#'+fileId), errorDiv = li.find('p.error'); - if ( !errorDiv.length ) { - errorDiv = $('

').appendTo( li ); - } - errorDiv.text(msg); - } - uploader.on('uploadSuccess', function( file, response ) { - //$( '#'+file.id ).addClass('upload-state-done'); - if (typeof response == 'object') { - if (response.error == 0) { - var value = $('#{$fileId}').val(); - var item = response.url + ',' + file.name; - if (value != '') { - value = value + ';' + item; - } else { - value = item; - } - $('#{$fileId}').val(value); - } else { - showError(file.id, response.message); - } - } else { - showError(file.id, '上传失败'); - } - }); - uploader.on('uploadError', function( file ) { - showError(file.id, '上传失败'); - }); - uploader.on('uploadComplete', function( file ) { - $( '#'+file.id ).find('.progress').remove(); - }); -}); - -EOT; - return $html; - } -} - -class FormFile extends FormFiles { - public function renderInput() { - $this->maxCount(1); - return parent::renderInput(); - } -} - -class FormValueStyle extends FormGroup { - public $classStyle = 'input-xlarge'; - public $styles = []; - - public function mini() { - $this->classStyle = 'input-small'; - return $this; - } - - public function small() { - $this->classStyle = 'input-small'; - return $this; - } - - public function medium() { - $this->classStyle = 'input-medium'; - return $this; - } - - public function large() { - $this->classStyle = 'input-large'; - return $this; - } - - public function xLarge() { - $this->classStyle = 'input-xlarge'; - return $this; - } - - public function xxLarge() { - $this->classStyle = 'input-xxlarge'; - return $this; - } - - public function getStyle() { - $style = ''; - if (!empty($this->styles)) { - $style = ' style="'; - foreach ($this->styles as $k => $v) { - $style .= "$k:$v;"; - } - $style .= '"'; - } - return $style; - } - - public function width($width) { - $this->styles['width'] = $width + 'px'; - return $this; - } - - public function height($height) { - $this->styles['height'] = $height + 'px'; - return $this; - } - public function style($name, $value) { - $this->styles[$name] = $value; - return $this; - } -} - -class FormText extends FormValueStyle { - public $placeholder = ''; - - public function placeholder($placeholder) { - $this->placeholder = $placeholder; - return $this; - } - - public function renderInput() { - $style = $this->getStyle(); - $readonly = $this->readonly ? ' readonly="readonly"' : ''; - return "key}\" type=\"text\" name=\"n_{$this->key}\" placeholder=\"{$this->placeholder}\" value=\"{$this->value}\" class=\"{$this->classStyle}\"{$style}{$readonly} /> \n"; - } - - - -} - -class FormTextarea extends FormText { - public function renderInput() { - $style = $this->getStyle(); - $readonly = $this->readonly ? ' readonly="readonly"' : ''; - return " \n"; - } -} - -class FormRichText extends FormTextarea { - public function renderInput() { - if (!isset($this->styles['width'])) { - $this->style('width', '100%'); - } - if (!isset($this->styles['height'])) { - $this->style('height', '500px'); - } - $html = parent::renderInput(); - $html .= << -var editor_{$this->key}; -$('body').ready(function() { - KindEditor.ready(function(K) { - editor_{$this->key} = K.create('#i_{$this->key}', { - allowFileManager : false - }); - }); -}); - -EOT; - return $html; - } -} - -class FormSelect extends FormValueStyle { - public $options = []; - public function __construct($key='', $options=[]) { - $this->key($key); - $this->options($options); - } - - public function options($options) { - if (is_array($options)) { - foreach ($options as $ok => $ov) { - if (is_string($ok)) { - $this->options[] = [ - 'name' => $ok, - 'value' => $ov - ]; - } else if (is_string($ov)) { - $this->options[] = [ - 'name' => $ov, - 'value' => $ov - ]; - } else if (is_array($ov) && isset($ov['value'])) { - //fixed - if (isset($ov['key'])) { - $ov['name'] = $ov['key']; - } - if (isset($ov['name'])) { - $this->options[] = [ - 'name' => $ov['name'], - 'value' => $ov['value'] - ]; - } - } else { - continue; - } - } - } - return $this; - } - - public function renderInput() { - $style = $this->getStyle(); - $readonly = $this->readonly ? ' readonly="readonly"' : ''; - $html = " \n"; - return $html; - } -} -// \ No newline at end of file diff --git a/src/Fasim/Widget/Table.php b/src/Fasim/Widget/Table.php deleted file mode 100644 index c1be4f1..0000000 --- a/src/Fasim/Widget/Table.php +++ /dev/null @@ -1,614 +0,0 @@ -intval(); - $this->pager = new Pager(); - $this->pager->pageSize = 20; - $this->pager->style = Pager::Bootstrap; - $this->pager->url = '?page={page}'; - foreach ($_GET as $k => $v) { - if ($k === 'page') { - $this->pager->page = intval($v); - } else if (strlen($k) > 2 && substr($k, 0, 2) == 's_') { - $qk = substr($k, 2); - $this->querys[$qk] = $v; - } - } - } - - public function addField($field) { - $this->fields[] = $field; - return $this; - } - - public function addSearch($search) { - $this->searchs[] = $search; - return $this; - } - public function addButton($button) { - $this->buttons[] = $button; - return $this; - } - - public function newTextField($name, $key, $width=0) { - return new TextField($name, $key, $width); - } - - public function newLinkField($name, $key, $url, $width=0) { - return new LinkField($name, $key, $url, $width); - } - - public function newImageField($name, $key, $width=0) { - return new ImageField($name, $key, $width); - } - - public function newSelectSearch($key, $values) { - return new SelectSearch($key, $values); - } - - public function newTextSearch($key, $placeholder) { - return new TextSearch($key, $placeholder); - } - - public function newLinkButton($name, $url) { - return new LinkButton($name, $url); - } - - public function newButtonGroup($buttons=[]) { - return new ButtonGroup($buttons); - } - - public function addOperation($operations) { - $this->operations[] = $operations; - return $this; - } - - public function newLinkOperation($name='', $url='') { - return new LinkOperation($name, $url); - } - - public function data($data) { - $this->data = $data; - return $this; - } - - public function query($key = '') { - if ($key != '') { - return $this->querys[$key]; - } - return $this->querys; - } - - public function page() { - return $this->pager->page; - } - - public function totalCount($totalCount = -1) { - if ($totalCount == -1) { - return $this->pager->totalCount; - } - $this->pager->totalCount = $totalCount; - return $this; - } - - public function pageUrl($url) { - $this->pager->url = $this->getAdminUrl($url); - return $this; - } - - public function pageSize($pageSize = -1) { - if ($pageSize == -1) { - return $this->pager->pageSize; - } - $this->pager->pageSize = $pageSize; - return $this; - } - - - public function build() { - $nl = " \n"; - - //search - $search = ''; - if (count($this->searchs) > 0) { - $search = ''.$nl; - } - - //buttons - $buttons = ''; - if (count($this->buttons) > 0) { - $buttons = '
'.$nl; - foreach ($this->buttons as $button) { - $buttons .= $button->render(); - } - $buttons .= '
'.$nl; - } - - - //list - $list = ''.$nl; - $list .= ''.$nl; - $list .= ''.$nl; - foreach ($this->fields as $field) { - $widthAttr = $field->width == 0 ? '' : ' width="' . $field->width .'"'; - $list .= "{$field->name} \n"; - } - if (count($this->operations) > 0) { - $list .= " \n"; - } - $list .= ''.$nl; - $list .= ''.$nl; - $list .= ''.$nl; - foreach ($this->data as $row) { - $list .= ''.$nl; - foreach ($this->fields as $field) { - $keys = is_array($field->key) ? $field->key : [$field->key]; - $values = []; - foreach ($keys as $key) { - $ks = explode('.', $key); - $v = $row; - while (count($ks) > 0) { - $k = array_shift($ks); - if (is_object($v) && isset($v->$k)) { - $v = $v->$k; - } else if (is_array($row) && isset($v[$k])) { - $v = $v[$k]; - } else { - $v = ''; - } - } - $values[$key] = $v; - } - $field->value = is_array($field->key) ? $values : $values[$field->key]; - $alignStyle = ''; - if ($field->textAlign != '') { - $alignStyle = ' style="text-align:' . $field->textAlign . '"'; - } - $list .= " \n" . $field->render() . " \n ".$nl; - } - if (count($this->operations) > 0) { - $list .= " \n"; - } - $list .= ''.$nl; - } - $list .= ''.$nl; - $list .= '
操作
\n"; - for ($oi = 0; $oi < count($this->operations); $oi++) { - $opt = $this->operations[$oi]; - $opt->data = $row; - - if ($oi > 0) { - $list .= "  |  "; - } - $list .= $opt->render().$nl;; - } - $list .= "
'.$nl; - - //pagination - $pagination = ''.$nl; - - - return [ - 'search' => $search, - 'buttons' => $buttons, - 'list' => $list, - 'pagination' => $pagination - ]; - } - - public static function getAdminUrl($url) { - if ($url{0} != '#' && (strlen($url) < 4 || substr($url, 0, 4) != 'http')) { - $adminUrl = Config::baseUrl(); - if ($url{0} == '/') { - $url = substr($url, 0, 1); - } - $url = $adminUrl.$url; - } - return $url; - } - - public static function getImageUrl($url, $format='') { - if (strlen($url) < 4 || substr($url, 0, 4) != 'http') { - $url = Config::get('url.cdn').$url; - if ($format != '') { - $url .= '-'.$format.'.jpg'; - } - } - return $url; - } - -} - -abstract class Field { - public $name; - public $key; - public $value = ''; - public $width = 0; - - public function __construct($name, $key, $width = 0) { - $this->name = $name; - $this->key = $key; - $this->width = $width; - } - - public function name($name) { - $this->name = $name; - return $this; - } - - public function key($key) { - $this->key = $key; - return $this; - } - - abstract function render(); - -} - -class TextField extends Field { - public $textAlign = ''; - public $color = ''; - private $useSwitch = false; - private $cases = []; - private $default = ''; - private $callback = null; - private $vars = []; - - public function textAlign($textAlign) { - $this->textAlign = $textAlign; - return $this; - } - - public function color($color) { - $this->color = $color; - return $this; - } - - public function valueCase($c, $v) { - $this->useSwitch = true; - $this->cases[] = [ - 'c' => $c, - 'v' => $v - ]; - return $this; - } - - public function valueMap($map) { - $this->useSwitch = true; - foreach ($map as $c => $v) { - $this->cases[] = [ - 'c' => $c, - 'v' => $v - ]; - } - return $this; - } - - public function valueDefault($v) { - $this->useSwitch = true; - $this->default = $v; - return $this; - } - - public function assign($k, $v) { - $this->vars[$k] = $v; - return $this; - } - - public function callback($callback) { - $this->callback = $callback; - return $this; - } - - public function render() { - $value = $this->value; - if ($this->useSwitch) { - $value = $this->default; - foreach ($this->cases as $case) { - if ($case['c'] === $this->value) { - $value = $case['v']; - break; - } - } - } - if ($this->callback != null && is_callable($this->callback)) { - $callback = $this->callback; - $values = is_array($value) ? array_values($value) : [ $value ]; - if (count($this->vars) > 0) { - $values[] = $this->vars; - } - $value = $callback(...$values); - } - if ($this->color != '') { - $value = "color};\">$value"; - } - - return $value; - } - -} - -class LinkField extends TextField { - public $url = ''; - - public function __construct($name, $key, $url, $width = 0) { - $this->name = $name; - $this->key = $key; - $this->url = $url; - $this->width = $width; - } - - public function url($url) { - $this->url = $url; - return $this; - } - - public function render() { - - } -} - -class ImageField extends TextField { - public function render() { - $value = parent::render(); - $url = Table::getImageUrl($value); - return ""; - } -} - -abstract class Search { - public $key; - public $value; - public function __construct($key) { - $this->key = $key; - return $this; - } - public function key($key) { - $this->key = $key; - return $this; - } - public function value($value) { - $this->value = $value; - return $this; - } - abstract function render(); -} - -class TextSearch extends Search { - public $placeholder; - - public function __construct($key, $placeholder) { - $this->key = $key; - $this->placeholder = $placeholder; - } - - public function placeholder($placeholder) { - $this->placeholder = $placeholder; - return $this; - } - - public function render() { - $key = 's_'.$this->key; - return "value}\" type=\"text\" placeholder=\"{$this->placeholder}\" /> \n"; - } -} - -class SelectSearch extends Search { - public $values; - - public function __construct($key, $values) { - $this->key = $key; - $this->values = $values; - } - - public function values($values) { - $this->values = $values; - return $this; - } - - public function render() { - $key = 's_'.$this->key; - $nl = "\n"; - $html = "'.$nl; - return $html; - } -} - -class ButtonGroup { - public $buttons; - - public function __construct($buttons=[]) { - $this->buttons = $buttons; - } - - public function add($button) { - $this->buttons[] = $button; - return $this; - } - - public function render() { - if (count($this->buttons) > 0) { - $nl = "\n"; - $buttons = '
'.$nl; - foreach ($this->buttons as $button) { - $buttons .= $button->render(); - } - $buttons .= '
'.$nl; - } - return $buttons; - } -} - -class LinkButton { - public $name; - public $url; - public $buttonStyle = 'primary'; - public $iconStyle = 'plus'; - - public function __construct($name, $url) { - $this->name = $name; - $this->url = $url; - } - - public function name($name) { - $this->name = $name; - return $this; - } - - public function url($url) { - $this->url = $url; - return $this; - } - - public function buttonStyle($buttonStyle) { - $this->buttonStyle = $buttonStyle; - return $this; - } - - public function iconStyle($iconStyle) { - $this->iconStyle = $iconStyle; - return $this; - } - - public function render() { - $buttonStyle = $this->buttonStyle == '' ? '' : ' btn-'.$this->buttonStyle; - $icon = $this->iconStyle == '' ? '' : "iconStyle}\">"; - $url = Table::getAdminUrl($this->url); - return ""; - } -} - -class LinkOperation { - public $name; - public $url; - public $data; - public $classes = []; - public $attrs = []; - private $callback = null; - - public function __construct($name='', $url='') { - $this->name = $name; - $this->url = $url; - } - - public function name($name) { - $this->name = $name; - return $this; - } - - public function url($url) { - $this->url = $url; - return $this; - } - - public function callback($callback) { - $this->callback = $callback; - return $this; - } - - public function confirm($confirm) { - return $this->className('confirm-link')->attr('data-confirm', $confirm); - } - - public function className($name) { - $this->classes[] = $name; - return $this; - } - - public function attr($name, $value) { - $this->attrs = [$name => $value]; - return $this; - } - - public function getData($key) { - if ($this->data == null) { - return ''; - } - $keys = explode('.', $key); - $v = $this->data; - while (count($keys) > 0) { - $k = array_shift($keys); - if (is_object($v) && isset($v->$k)) { - return $v->$k; - } else if (is_array($v) && isset($v[$k])) { - return $v[$k]; - } else { - $v = ''; - } - } - return $v; - } - - public function filterValue($v) { - return preg_replace_callback('/\{(\w+)\}/', function ($matches) { - return $this->getData($matches[1]); - }, $v); - } - - public function render() { - if ($this->callback != null && is_callable($this->callback)) { - $callback = $this->callback; - $result = $callback($this->data); - if (is_array($result)) { - list($this->name, $this->url, $this->attrs) = $result; - if (empty($this->attrs)) { - $this->attrs = []; - } - } else if (is_string($result)) { - $this->name = $result; - } - } - $url = Table::getAdminUrl($this->url); - $url = $this->filterValue($url); - - $attrs = $this->attrs; - $attrAppend = ''; - if (count($this->classes) > 0) { - $old = isset($attrs['class']) ? $attrs['class'].' ' : ''; - $attrs['class'] = $old.implode(' ', $this->classes); - } - if (count($attrs) > 0) { - foreach ($attrs as $an => $av) { - $attrAppend .= ' ' . $an . '="' . $this->filterValue($av) . '"'; - } - } - - - return " {$this->name} "; - } -} diff --git a/src/tests/config/domain.php b/src/tests/config/domain.php new file mode 100644 index 0000000..d266508 --- /dev/null +++ b/src/tests/config/domain.php @@ -0,0 +1,2 @@ +