Skip to content

Commit 4b94f36

Browse files
committed
Merge branch 'main' of github.com:mevdschee/php-crud-api into main
2 parents 3d7b3d9 + 52e1868 commit 4b94f36

File tree

6 files changed

+78
-104
lines changed

6 files changed

+78
-104
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ Related projects:
134134
- [PHP-CRUD-UI](https://github.com/mevdschee/php-crud-ui): Single file PHP script that adds a UI to a PHP-CRUD-API project.
135135
- [PHP-CRUD-ADMIN](https://github.com/mevdschee/php-crud-admin): Single file PHP script that adds a database admin interface to a PHP-CRUD-API project.
136136
- [PHP-SP-API](https://github.com/mevdschee/php-sp-api): Single file PHP script that adds a REST API to a SQL database.
137-
- [VUE-CRUD-UI](https://github.com/nlware/vue-crud-ui): Single file Vue.js script that adds a UI to a PHP-CRUD-API project.
138137
- [ra-data-treeql](https://github.com/nkappler/ra-data-treeql): NPM package that provides a [Data Provider](https://marmelab.com/react-admin/DataProviderIntroduction.html) for [React Admin](https://marmelab.com/react-admin/).
138+
- [scriptPilot/vueuse](https://github.com/scriptPilot/vueuse/): Vue [Composables](https://vuejs.org/guide/reusability/composables.html) in addition to [VueUse.org](https://vueuse.org/) (that support PHP-CRUD-API).
139+
- [VUE-CRUD-UI](https://github.com/nlware/vue-crud-ui): Single file Vue.js script that adds a UI to a PHP-CRUD-API project.
139140

140141
There are also ports of this script in:
141142

api.include.php

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,16 +1515,18 @@ public function createStream(string $content = ''): StreamInterface
15151515

15161516
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
15171517
{
1518-
if ('' === $filename) {
1519-
throw new \RuntimeException('Path cannot be empty');
1518+
try {
1519+
$resource = @\fopen($filename, $mode);
1520+
} catch (\Throwable $e) {
1521+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
15201522
}
15211523

1522-
if (false === $resource = @\fopen($filename, $mode)) {
1524+
if (false === $resource) {
15231525
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
15241526
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
15251527
}
15261528

1527-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? ''));
1529+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
15281530
}
15291531

15301532
return Stream::create($resource);
@@ -2141,9 +2143,6 @@ public function getAttributes(): array
21412143
return $this->attributes;
21422144
}
21432145

2144-
/**
2145-
* @return mixed
2146-
*/
21472146
public function getAttribute($attribute, $default = null)
21482147
{
21492148
if (false === \array_key_exists($attribute, $this->attributes)) {
@@ -2359,20 +2358,16 @@ public function getSize() /*:?int*/
23592358

23602359
public function tell(): int
23612360
{
2362-
if (!isset($this->stream)) {
2363-
throw new \RuntimeException('Stream is detached');
2364-
}
2365-
2366-
if (false === $result = @\ftell($this->stream)) {
2367-
throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? ''));
2361+
if (false === $result = \ftell($this->stream)) {
2362+
throw new \RuntimeException('Unable to determine stream position');
23682363
}
23692364

23702365
return $result;
23712366
}
23722367

23732368
public function eof(): bool
23742369
{
2375-
return !isset($this->stream) || \feof($this->stream);
2370+
return !$this->stream || \feof($this->stream);
23762371
}
23772372

23782373
public function isSeekable(): bool
@@ -2382,10 +2377,6 @@ public function isSeekable(): bool
23822377

23832378
public function seek($offset, $whence = \SEEK_SET) /*:void*/
23842379
{
2385-
if (!isset($this->stream)) {
2386-
throw new \RuntimeException('Stream is detached');
2387-
}
2388-
23892380
if (!$this->seekable) {
23902381
throw new \RuntimeException('Stream is not seekable');
23912382
}
@@ -2407,19 +2398,15 @@ public function isWritable(): bool
24072398

24082399
public function write($string): int
24092400
{
2410-
if (!isset($this->stream)) {
2411-
throw new \RuntimeException('Stream is detached');
2412-
}
2413-
24142401
if (!$this->writable) {
24152402
throw new \RuntimeException('Cannot write to a non-writable stream');
24162403
}
24172404

24182405
// We can't know the size after writing anything
24192406
$this->size = null;
24202407

2421-
if (false === $result = @\fwrite($this->stream, $string)) {
2422-
throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? ''));
2408+
if (false === $result = \fwrite($this->stream, $string)) {
2409+
throw new \RuntimeException('Unable to write to stream');
24232410
}
24242411

24252412
return $result;
@@ -2432,16 +2419,12 @@ public function isReadable(): bool
24322419

24332420
public function read($length): string
24342421
{
2435-
if (!isset($this->stream)) {
2436-
throw new \RuntimeException('Stream is detached');
2437-
}
2438-
24392422
if (!$this->readable) {
24402423
throw new \RuntimeException('Cannot read from non-readable stream');
24412424
}
24422425

2443-
if (false === $result = @\fread($this->stream, $length)) {
2444-
throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? ''));
2426+
if (false === $result = \fread($this->stream, $length)) {
2427+
throw new \RuntimeException('Unable to read from stream');
24452428
}
24462429

24472430
return $result;
@@ -2450,19 +2433,16 @@ public function read($length): string
24502433
public function getContents(): string
24512434
{
24522435
if (!isset($this->stream)) {
2453-
throw new \RuntimeException('Stream is detached');
2436+
throw new \RuntimeException('Unable to read stream contents');
24542437
}
24552438

2456-
if (false === $contents = @\stream_get_contents($this->stream)) {
2457-
throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? ''));
2439+
if (false === $contents = \stream_get_contents($this->stream)) {
2440+
throw new \RuntimeException('Unable to read stream contents');
24582441
}
24592442

24602443
return $contents;
24612444
}
24622445

2463-
/**
2464-
* @return mixed
2465-
*/
24662446
public function getMetadata($key = null)
24672447
{
24682448
if (!isset($this->stream)) {
@@ -2559,7 +2539,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
25592539

25602540
if (\UPLOAD_ERR_OK === $this->error) {
25612541
// Depending on the value set file or stream variable.
2562-
if (\is_string($streamOrFile) && '' !== $streamOrFile) {
2542+
if (\is_string($streamOrFile)) {
25632543
$this->file = $streamOrFile;
25642544
} elseif (\is_resource($streamOrFile)) {
25652545
$this->stream = Stream::create($streamOrFile);
@@ -2593,11 +2573,11 @@ public function getStream(): StreamInterface
25932573
return $this->stream;
25942574
}
25952575

2596-
if (false === $resource = @\fopen($this->file, 'r')) {
2597-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
2576+
try {
2577+
return Stream::create(\fopen($this->file, 'r'));
2578+
} catch (\Throwable $e) {
2579+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
25982580
}
2599-
2600-
return Stream::create($resource);
26012581
}
26022582

26032583
public function moveTo($targetPath) /*:void*/
@@ -2609,23 +2589,20 @@ public function moveTo($targetPath) /*:void*/
26092589
}
26102590

26112591
if (null !== $this->file) {
2612-
$this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);
2613-
2614-
if (false === $this->moved) {
2615-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
2616-
}
2592+
$this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
26172593
} else {
26182594
$stream = $this->getStream();
26192595
if ($stream->isSeekable()) {
26202596
$stream->rewind();
26212597
}
26222598

2623-
if (false === $resource = @\fopen($targetPath, 'w')) {
2624-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
2599+
try {
2600+
// Copy the contents of a stream into another stream until end-of-file.
2601+
$dest = Stream::create(\fopen($targetPath, 'w'));
2602+
} catch (\Throwable $e) {
2603+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath));
26252604
}
26262605

2627-
$dest = Stream::create($resource);
2628-
26292606
while (!$stream->eof()) {
26302607
if (!$dest->write($stream->read(1048576))) {
26312608
break;
@@ -2634,6 +2611,10 @@ public function moveTo($targetPath) /*:void*/
26342611

26352612
$this->moved = true;
26362613
}
2614+
2615+
if (false === $this->moved) {
2616+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
2617+
}
26372618
}
26382619

26392620
public function getSize(): int
@@ -9893,6 +9874,7 @@ class OpenApiBuilder
98939874
private $openapi;
98949875
private $records;
98959876
private $columns;
9877+
private $status;
98969878
private $builders;
98979879

98989880
public function __construct(ReflectionService $reflection, array $base, array $controllers, array $builders)

0 commit comments

Comments
 (0)