mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
bug fixes and better response handling
This commit is contained in:
parent
7eb8322d71
commit
5965f2a4ca
|
|
@ -194,48 +194,19 @@ abstract class GrammarAbstract
|
|||
*
|
||||
* @param array $elements Elements
|
||||
* @param string $prefix Prefix for table
|
||||
* @param string $column Is always column?
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected function expressionizeTableColumn(array $elements, string $prefix = '') : string
|
||||
{
|
||||
$expression = '';
|
||||
|
||||
foreach ($elements as $key => $element) {
|
||||
if (\is_string($element) && $element !== '*') {
|
||||
$expression .= $this->compileSystem($element, $prefix) . ', ';
|
||||
} elseif (\is_string($element) && $element === '*') {
|
||||
$expression .= '*, ';
|
||||
} elseif ($element instanceof \Closure) {
|
||||
$expression .= $element() . ', ';
|
||||
} elseif ($element instanceof BuilderAbstract) {
|
||||
$expression .= $element->toSql() . ', ';
|
||||
} else {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
return \rtrim($expression, ', ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Expressionize elements.
|
||||
*
|
||||
* @param array $elements Elements
|
||||
* @param string $prefix Prefix for table
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected function expressionizeTable(array $elements, string $prefix = '') : string
|
||||
protected function expressionizeTableColumn(array $elements, string $prefix = '', bool $column = true) : string
|
||||
{
|
||||
$expression = '';
|
||||
|
||||
foreach ($elements as $key => $element) {
|
||||
if (\is_string($element) && $element !== '*') {
|
||||
$prefix = \stripos($element, '.') !== false || $column ? $prefix : '';
|
||||
$expression .= $this->compileSystem($element, $prefix) . ', ';
|
||||
} elseif (\is_string($element) && $element === '*') {
|
||||
$expression .= '*, ';
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ class Grammar extends GrammarAbstract
|
|||
*/
|
||||
protected function compileSelects(Builder $query, array $columns) : string
|
||||
{
|
||||
$expression = $this->expressionizeTableColumn($columns, $query->getPrefix());
|
||||
$expression = $this->expressionizeTableColumn($columns, $query->getPrefix(), false);
|
||||
|
||||
if ($expression === '') {
|
||||
$expression = '*';
|
||||
|
|
@ -151,7 +151,7 @@ class Grammar extends GrammarAbstract
|
|||
*/
|
||||
protected function compileUpdates(Builder $query, array $table) : string
|
||||
{
|
||||
$expression = $this->expressionizeTable($table, $query->getPrefix());
|
||||
$expression = $this->expressionizeTableColumn($table, $query->getPrefix());
|
||||
|
||||
if ($expression === '') {
|
||||
return '';
|
||||
|
|
@ -187,7 +187,7 @@ class Grammar extends GrammarAbstract
|
|||
*/
|
||||
protected function compileFrom(Builder $query, array $table) : string
|
||||
{
|
||||
$expression = $this->expressionizeTable($table, $query->getPrefix());
|
||||
$expression = $this->expressionizeTableColumn($table, $query->getPrefix());
|
||||
|
||||
if ($expression === '') {
|
||||
return '';
|
||||
|
|
@ -245,12 +245,10 @@ class Grammar extends GrammarAbstract
|
|||
}
|
||||
|
||||
if (\is_string($element['column'])) {
|
||||
// handle bug when no table is specified in the where column
|
||||
if (\count($query->from) === 1 && \stripos($element['column'], '.') === false) {
|
||||
$element['column'] = $query->from[0] . '.' . $element['column'];
|
||||
}
|
||||
// No prefix if it is ONLY a field
|
||||
$prefix = \stripos($element['column'], '.') === false ? '' : $query->getPrefix();
|
||||
|
||||
$expression .= $this->compileSystem($element['column'], $query->getPrefix());
|
||||
$expression .= $this->compileSystem($element['column'], $prefix);
|
||||
} elseif ($element['column'] instanceof \Closure) {
|
||||
$expression .= $element['column']();
|
||||
} elseif ($element['column'] instanceof Builder) {
|
||||
|
|
@ -318,6 +316,12 @@ class Grammar extends GrammarAbstract
|
|||
return $this->compileSystem($value->getColumn(), $prefix);
|
||||
} elseif ($value instanceof Builder) {
|
||||
return '(' . \rtrim($value->toSql(), ';') . ')';
|
||||
} elseif ($value instanceof \JsonSerializable) {
|
||||
$encoded = \json_encode($value);
|
||||
|
||||
return $encoded ? $encoded : null;
|
||||
} elseif ($value instanceof \Serializable) {
|
||||
return $element->serialize();
|
||||
} else {
|
||||
throw new \InvalidArgumentException(\gettype($value));
|
||||
}
|
||||
|
|
@ -591,8 +595,7 @@ class Grammar extends GrammarAbstract
|
|||
$vals = '';
|
||||
|
||||
foreach ($values as $column => $value) {
|
||||
// todo change expressionizeTableColumn to accept single column and create additionl for Columns
|
||||
$expression = $this->expressionizeTableColumn([$column], $query->getPrefix());
|
||||
$expression = $this->expressionizeTableColumn([$column], $query->getPrefix(), false);
|
||||
|
||||
$vals .= $expression . ' = ' . $this->compileValue($query, $value) . ', ';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ class Grammar extends QueryGrammar
|
|||
*/
|
||||
protected function compileCreateTable(BuilderAbstract $query, string $table) : string
|
||||
{
|
||||
return 'CREATE TABLE ' . $this->expressionizeTable([$table], $query->getPrefix());
|
||||
return 'CREATE TABLE ' . $this->expressionizeTableColumn([$table], $query->getPrefix());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ class MysqlGrammar extends Grammar
|
|||
&& !empty($field['foreignTable']) && !empty($field['foreignKey'])
|
||||
) {
|
||||
$keys .= ' FOREIGN KEY (' . $this->expressionizeTableColumn([$name], '') . ') REFERENCES '
|
||||
. $this->expressionizeTable([$field['foreignTable']], $query->getPrefix())
|
||||
. $this->expressionizeTableColumn([$field['foreignTable']], $query->getPrefix())
|
||||
. ' (' . $this->expressionizeTableColumn([$field['foreignKey']], '') . '),';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,14 +134,18 @@ final class Header extends HeaderAbstract
|
|||
/**
|
||||
* Get header by name.
|
||||
*
|
||||
* @param string $key Header key
|
||||
* @param null|string $key Header key
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get(string $key) : array
|
||||
public function get(string $key = null) : array
|
||||
{
|
||||
if ($key === null) {
|
||||
return $this->header;
|
||||
}
|
||||
|
||||
return $this->header[\strtolower($key)] ?? [];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,19 +90,21 @@ final class Response extends ResponseAbstract implements RenderableInterface
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBody() : string
|
||||
public function getBody(bool $optimize = false) : string
|
||||
{
|
||||
return $this->render();
|
||||
return $this->render($optimize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate response based on header.
|
||||
*
|
||||
* @param bool $optimize Optimize response / minify
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function render() : string
|
||||
public function render(bool $optimize = false) : string
|
||||
{
|
||||
$types = $this->header->get('Content-Type');
|
||||
|
||||
|
|
@ -112,19 +114,21 @@ final class Response extends ResponseAbstract implements RenderableInterface
|
|||
}
|
||||
}
|
||||
|
||||
return $this->getRaw();
|
||||
return $this->getRaw($optimize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate raw response.
|
||||
*
|
||||
* @param bool $optimize Optimize response / minify
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \Exception this exception is thrown if the response cannot be rendered
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function getRaw() : string
|
||||
private function getRaw(bool $optimize = false) : string
|
||||
{
|
||||
$render = '';
|
||||
|
||||
|
|
|
|||
|
|
@ -207,7 +207,7 @@ abstract class HeaderAbstract
|
|||
/**
|
||||
* Get header by key.
|
||||
*
|
||||
* @param string $key Header key
|
||||
* @param null|string $key Header key
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ final class Header extends HeaderAbstract
|
|||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->set('Content-Type', 'text/html; charset=utf-8');
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,27 +90,29 @@ final class Response extends ResponseAbstract implements RenderableInterface
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBody() : string
|
||||
public function getBody(bool $optimize = false) : string
|
||||
{
|
||||
return $this->render();
|
||||
return $this->render($optimize);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getJson() : array
|
||||
public function getJsonData() : array
|
||||
{
|
||||
return \json_decode($this->render(), true);
|
||||
return \json_decode($this->getRaw(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate response based on header.
|
||||
*
|
||||
* @param bool $optimize Optimize response / minify
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function render() : string
|
||||
public function render(bool $optimize = false) : string
|
||||
{
|
||||
$types = $this->header->get('Content-Type');
|
||||
|
||||
|
|
@ -120,19 +122,21 @@ final class Response extends ResponseAbstract implements RenderableInterface
|
|||
}
|
||||
}
|
||||
|
||||
return $this->getRaw();
|
||||
return $this->getRaw($optimize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate raw response.
|
||||
*
|
||||
* @param bool $optimize Optimize response / minify
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function getRaw() : string
|
||||
private function getRaw(bool $optimize = false) : string
|
||||
{
|
||||
$render = '';
|
||||
|
||||
|
|
@ -140,7 +144,11 @@ final class Response extends ResponseAbstract implements RenderableInterface
|
|||
$render .= StringUtils::stringify($response);
|
||||
}
|
||||
|
||||
return $this->removeWhitespaceAndLineBreak($render);
|
||||
if ($optimize) {
|
||||
return $this->removeWhitespaceAndLineBreak($render);
|
||||
}
|
||||
|
||||
return $render;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -106,9 +106,11 @@ abstract class ResponseAbstract implements MessageInterface, \JsonSerializable
|
|||
/**
|
||||
* Get response body.
|
||||
*
|
||||
* @param bool $optimize Optimize response / minify
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract public function getBody() : string;
|
||||
abstract public function getBody(bool $optimize = false) : string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,14 +39,14 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
|
|||
public function testMysqlShowTables() : void
|
||||
{
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'SELECT `table_name` FROM `information_schema`.`tables` WHERE `information_schema`.`tables`.`table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\';';
|
||||
$sql = 'SELECT `table_name` FROM `information_schema`.`tables` WHERE `table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\';';
|
||||
self::assertEquals($sql, $query->selectTables()->toSql());
|
||||
}
|
||||
|
||||
public function testMysqlShowFields() : void
|
||||
{
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'SELECT * FROM `information_schema`.`columns` WHERE `information_schema`.`columns`.`table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\' AND `information_schema`.`columns`.`table_name` = \'test\';';
|
||||
$sql = 'SELECT * FROM `information_schema`.`columns` WHERE `table_schema` = \'' . $GLOBALS['CONFIG']['db']['core']['masters']['admin']['database']. '\' AND `table_name` = \'test\';';
|
||||
self::assertEquals($sql, $query->selectFields('test')->toSql());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ class HeaderAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
return true;
|
||||
}
|
||||
|
||||
public function get(string $key) : array
|
||||
public function get(string $key = null) : array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ class RequestTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
self::assertEquals(
|
||||
"The OMS License 1.0\n\nCopyright (c) <Dennis Eichhorn> All Rights Reserved\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.",
|
||||
$request->rest()
|
||||
$request->rest()->getBody()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ class RestTest extends \PHPUnit\Framework\TestCase
|
|||
$request = new Request(new Http('http://httpbin.org/post'));
|
||||
$request->setMethod(RequestMethod::POST);
|
||||
self::assertTrue($request->setData('pdata', 'abc'));
|
||||
self::assertEquals('abc', REST::request($request)->getJson()['form']['pdata']);
|
||||
self::assertEquals('abc', REST::request($request)->getJsonData()['form']['pdata']);
|
||||
}
|
||||
|
||||
public function testPut() : void
|
||||
|
|
@ -48,7 +48,7 @@ class RestTest extends \PHPUnit\Framework\TestCase
|
|||
$request = new Request(new Http('http://httpbin.org/put'));
|
||||
$request->setMethod(RequestMethod::PUT);
|
||||
self::assertTrue($request->setData('pdata', 'abc'));
|
||||
self::assertEquals('abc', REST::request($request)->getJson()['form']['pdata']);
|
||||
self::assertEquals('abc', REST::request($request)->getJsonData()['form']['pdata']);
|
||||
}
|
||||
|
||||
public function testDelete() : void
|
||||
|
|
@ -56,7 +56,7 @@ class RestTest extends \PHPUnit\Framework\TestCase
|
|||
$request = new Request(new Http('http://httpbin.org/delete'));
|
||||
$request->setMethod(RequestMethod::DELETE);
|
||||
self::assertTrue($request->setData('ddata', 'abc'));
|
||||
self::assertEquals('abc', REST::request($request)->getJson()['form']['ddata']);
|
||||
self::assertEquals('abc', REST::request($request)->getJsonData()['form']['ddata']);
|
||||
}
|
||||
|
||||
public function testGet() : void
|
||||
|
|
@ -64,6 +64,6 @@ class RestTest extends \PHPUnit\Framework\TestCase
|
|||
$request = new Request(new Http('http://httpbin.org/get'));
|
||||
$request->setMethod(RequestMethod::GET);
|
||||
self::assertTrue($request->setData('gdata', 'abc'));
|
||||
self::assertEquals('abc', REST::request($request)->getJson()['args']['gdata']);
|
||||
self::assertEquals('abc', REST::request($request)->getJsonData()['args']['gdata']);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class ResponseAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
return [1];
|
||||
}
|
||||
|
||||
public function getBody() : string
|
||||
public function getBody(bool $optimize = false) : string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user