Skip to content

Commit 1c8e565

Browse files
author
Steeven Andrian
committed
Orm, System controllers and models, cli update
1 parent 46f5a3e commit 1c8e565

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6025
-770
lines changed

src/Cli/Commanders/Serve.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ public function optionPort($port)
110110
*/
111111
public function execute()
112112
{
113-
$options = input()->get();
113+
$this->__callOptions();
114114

115-
if (empty($options)) {
116-
$_GET[ 'host' ] = 'localhost';
117-
$_GET[ 'port' ] = 8000;
115+
if(empty($this->optionHost)) {
116+
$this->optionHost = 'localhost';
118117
}
119118

120-
$this->__callOptions();
119+
if(empty($this->optionPort)) {
120+
$this->optionPort = 8000;
121+
}
121122

122123
output()->write(
123124
(new Format())

src/Framework.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ private function httpHandler()
443443
if (profiler() !== false) {
444444
profiler()->watch('Parse Router Request');
445445
}
446+
446447
router()->handle(new Uri());
447448

448449
if (config()->loadFile('session') === true) {
@@ -452,7 +453,10 @@ private function httpHandler()
452453
$session->setLogger($this->services->get('logger'));
453454

454455
if ( ! $session->isStarted()) {
455-
$session->start();
456+
$firstSegment = server_request()->getUri()->segments->first();
457+
if( ! in_array($firstSegment, ['resources', 'images', 'storage'])) {
458+
$session->start();
459+
}
456460
}
457461

458462
$this->services->add($session, 'session');

src/Helpers/Framework.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function config()
6060
*
6161
* Convenient shortcut for O2System Framework globals container.
6262
*
63-
* @return mixed|O2System\Framework\Containers\Globals
63+
* @return O2System\Framework\Containers\Globals
6464
*/
6565
function globals()
6666
{

src/Http/Controllers/Resources.php

+1-44
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @package O2System\Framework\Http\Controllers
2626
*/
27-
class Resources extends Controller
27+
class Resources extends Storage
2828
{
2929
/**
3030
* Resources::$inherited
@@ -65,47 +65,4 @@ public function __construct()
6565
{
6666
$this->directoryPath = PATH_RESOURCES;
6767
}
68-
69-
// ------------------------------------------------------------------------
70-
71-
/**
72-
* Resources::route
73-
*/
74-
public function route()
75-
{
76-
$segments = server_request()->getUri()->segments->getArrayCopy();
77-
array_shift($segments);
78-
79-
$download = false;
80-
if (false !== ($key = array_search('download', $segments))) {
81-
$download = true;
82-
unset($segments[ $key ]);
83-
$segments = array_values($segments);
84-
}
85-
86-
if (count($segments)) {
87-
$filePath = $this->directoryPath . implode(DIRECTORY_SEPARATOR, $segments);
88-
if (is_file($filePath)) {
89-
if ($download) {
90-
$downloader = new Downloader($filePath);
91-
$downloader
92-
->speedLimit($this->speedLimit)
93-
->resumeable($this->resumeable)
94-
->download();
95-
} else {
96-
$fileInfo = new SplFileInfo($filePath);
97-
header('Content-Disposition: filename=' . $fileInfo->getFilename());
98-
header('Content-Transfer-Encoding: binary');
99-
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
100-
header('Content-Type: ' . $fileInfo->getMime());
101-
echo @readfile($filePath);
102-
exit(EXIT_SUCCESS);
103-
}
104-
} else {
105-
redirect_url('error/404');
106-
}
107-
} else {
108-
redirect_url('error/403');
109-
}
110-
}
11168
}

src/Http/Controllers/Restful.php

+31-29
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,21 @@ public function index()
382382

383383
if (empty($this->getValidationRules)) {
384384
if ($get = input()->get()) {
385+
if($scopes = $get->offsetGet('scopes')) {
386+
$get->offsetUnset('scopes');
387+
$scopes = explode(',', $scopes);
388+
$scopes = array_map('trim', $scopes);
389+
$scopes = array_filter($scopes);
390+
391+
$this->model->appendColumns = $scopes;
392+
}
393+
385394
if (false !== ($result = $this->model->findWhere($get->getArrayCopy(), $limit))) {
386395
if ($result->count()) {
396+
if($get->offsetExists('id')) {
397+
$result = $result->first();
398+
}
399+
387400
$this->sendPayload($result);
388401
} else {
389402
$this->sendError(204);
@@ -397,22 +410,27 @@ public function index()
397410
$this->sendError(204);
398411
}
399412
} elseif ($get = input()->get()) {
400-
$get->validation($this->getValidationRules, $this->getValidationCustomErrors);
413+
if($scopes = $get->offsetGet('scopes')) {
414+
$get->offsetUnset('scopes');
415+
$scopes = explode(',', $scopes);
416+
$scopes = array_map('trim', $scopes);
417+
$scopes = array_filter($scopes);
401418

402-
if ( ! $get->validate()) {
403-
$this->sendError(400, implode(', ', $get->validator->getErrors()));
419+
$this->model->appendColumns = $scopes;
404420
}
405421

406-
$conditions = [];
422+
$get->validation($this->getValidationRules, $this->getValidationCustomErrors);
407423

408-
foreach ($this->getValidationRules as $field => $rule) {
409-
if($get->offsetExists($field)) {
410-
$conditions[ $field ] = $get->offsetGet($field);
411-
}
424+
if ( ! $get->validate()) {
425+
$this->sendError(400, implode(', ', $get->validator->getErrors()));
412426
}
413427

414-
if (false !== ($result = $this->model->findWhere($conditions, $limit))) {
428+
if (false !== ($result = $this->model->findWhere($get->getArrayCopy(), $limit))) {
415429
if ($result->count()) {
430+
if($get->offsetExists('id')) {
431+
$result = $result->first();
432+
}
433+
416434
$this->sendPayload($result);
417435
} else {
418436
$this->sendError(204);
@@ -544,14 +562,6 @@ public function create()
544562
}
545563
}
546564

547-
if (count($this->fillableColumns)) {
548-
foreach ($this->fillableColumns as $column) {
549-
if ($post->offsetExists($column)) {
550-
$data[ $column ] = $post->offsetGet($column);
551-
}
552-
}
553-
}
554-
555565
if (count($data)) {
556566
$data[ 'record_create_timestamp' ] = $data[ 'record_update_timestamp' ] = timestamp();
557567

@@ -654,14 +664,6 @@ public function update()
654664
}
655665
}
656666

657-
if (count($this->fillableColumns)) {
658-
foreach ($this->fillableColumns as $column) {
659-
if ($post->offsetExists($column)) {
660-
$data[ $column ] = $post->offsetGet($column);
661-
}
662-
}
663-
}
664-
665667
if (count($data)) {
666668
$data[ 'record_update_timestamp' ] = timestamp();
667669

@@ -756,7 +758,7 @@ public function delete($id = null)
756758

757759
if ($result = $this->model->findWhere($conditions)) {
758760
if ($result instanceof Row) {
759-
if ($row->delete()) {
761+
if ($result->delete()) {
760762
$this->sendError(200);
761763
} else {
762764
$this->sendError(501);
@@ -827,7 +829,7 @@ public function delete($id = null)
827829

828830
if ($result = $this->model->findWhere($conditions)) {
829831
if ($result instanceof Row) {
830-
if ($row->delete()) {
832+
if ($result->delete()) {
831833
$this->sendError(200);
832834
} else {
833835
$this->sendError(501);
@@ -905,7 +907,7 @@ private function updateRecordStatus(array $params, $method)
905907

906908
if ($result = $this->model->findWhere($conditions)) {
907909
if ($result instanceof Row) {
908-
if ($row->{$method}()) {
910+
if ($result->{$method}()) {
909911
$this->sendError(200);
910912
} else {
911913
$this->sendError(501);
@@ -979,7 +981,7 @@ private function updateRecordStatus(array $params, $method)
979981

980982
if ($result = $this->model->findWhere($conditions)) {
981983
if ($result instanceof Row) {
982-
if ($row->{$method}()) {
984+
if ($result->{$method}()) {
983985
$this->sendError(200);
984986
} else {
985987
$this->sendError(501);

src/Http/Controllers/Storage.php

+50-40
Original file line numberDiff line numberDiff line change
@@ -97,51 +97,61 @@ public function route()
9797
redirect_url('error/505');
9898
} else {
9999
$fileInfo = new SplFileInfo($filePath);
100-
$fileChunkSize = 1024*1024;
101-
$lengthStart = 0;
102-
$lengthEnd = $fileInfo->getSize();
103-
104-
if ($httpRange = input()->server('HTTP_RANGE')) {
105-
if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $httpRange, $matches)) {
106-
$lengthStart = intval($matches[ 0 ]);
107-
if ( ! empty($matches[ 1 ])) {
108-
$lengthEnd = intval($matches[ 1 ]);
100+
101+
if(in_array($fileInfo->getExtension(), ['json', 'txt', 'xml'])) {
102+
header('Content-Disposition: filename=' . $fileInfo->getFilename());
103+
header('Content-Transfer-Encoding: binary');
104+
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
105+
header('Content-Type: ' . $fileInfo->getMime());
106+
echo file_get_contents($filePath);
107+
exit;
108+
} else {
109+
$fileChunkSize = 1024*1024;
110+
$lengthStart = 0;
111+
$lengthEnd = $fileInfo->getSize();
112+
113+
if ($httpRange = input()->server('HTTP_RANGE')) {
114+
if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $httpRange, $matches)) {
115+
$lengthStart = intval($matches[ 0 ]);
116+
if ( ! empty($matches[ 1 ])) {
117+
$lengthEnd = intval($matches[ 1 ]);
118+
}
109119
}
110120
}
111-
}
112121

113-
if ($lengthStart > 0 || $lengthEnd < $fileInfo->getSize()) {
114-
header('HTTP/1.0 206 Partial Content');
115-
} else {
116-
header('HTTP/1.0 200 OK');
117-
}
122+
if ($lengthStart > 0 || $lengthEnd < $fileInfo->getSize()) {
123+
header('HTTP/1.0 206 Partial Content');
124+
} else {
125+
header('HTTP/1.0 200 OK');
126+
}
118127

119-
header('Content-Type: ' . $fileInfo->getMime());
120-
header('Cache-Control: no-cache, no-store, must-revalidate');
121-
header('Pragma: no-cache');
122-
header('Expires: 0');
123-
header('Content-Length:' . ($lengthEnd - $lengthStart));
124-
header("Content-Range: bytes " . $lengthStart-$lengthEnd/$fileInfo->getSize());
125-
header("Content-Disposition: inline; filename=" . $fileInfo->getFilename());
126-
header("Content-Transfer-Encoding: binary\n");
127-
header("Last-Modified: " . gmdate('D, d M Y H:i:s', $fileInfo->getMTime()) . ' GMT');
128-
header('Connection: close');
129-
130-
$lengthCurrent = $lengthStart;
131-
fseek($fileHandle, $lengthStart, 0);
132-
133-
$buffer = '';
134-
ob_start();
135-
while ( ! feof($fileHandle) && $lengthCurrent < $lengthEnd && (connection_status() == 0)) {
136-
echo fread($fileHandle, min($fileChunkSize, $lengthEnd - $lengthCurrent));
137-
$lengthCurrent += $fileChunkSize;
138-
$buffer.= ob_get_contents();
139-
140-
ob_end_flush();
141-
}
128+
header('Content-Type: ' . $fileInfo->getMime());
129+
header('Cache-Control: no-cache, no-store, must-revalidate');
130+
header('Pragma: no-cache');
131+
header('Expires: 0');
132+
header('Content-Length:' . ($lengthEnd - $lengthStart));
133+
header("Content-Range: bytes " . ($lengthStart-$lengthEnd)/$fileInfo->getSize());
134+
header("Content-Disposition: inline; filename=" . $fileInfo->getFilename() . '.' . $fileInfo->getExtension());
135+
header("Content-Transfer-Encoding: binary\n");
136+
header("Last-Modified: " . gmdate('D, d M Y H:i:s', $fileInfo->getMTime()) . ' GMT');
137+
header('Connection: close');
138+
139+
$lengthCurrent = $lengthStart;
140+
fseek($fileHandle, $lengthStart, 0);
141+
142+
$buffer = '';
143+
ob_start();
144+
while ( ! feof($fileHandle) && $lengthCurrent < $lengthEnd && (connection_status() == 0)) {
145+
echo fread($fileHandle, min($fileChunkSize, $lengthEnd - $lengthCurrent));
146+
$lengthCurrent += $fileChunkSize;
147+
$buffer.= ob_get_contents();
148+
149+
ob_end_flush();
150+
}
142151

143-
echo $buffer;
144-
exit(EXIT_SUCCESS);
152+
echo $buffer;
153+
exit;
154+
}
145155
}
146156
} else {
147157
redirect_url('error/404');

0 commit comments

Comments
 (0)