| 1 | <?php |
||
| 13 | trait Psr7 { |
||
| 14 | /** |
||
| 15 | * Initialize request from PSR-7 request object |
||
| 16 | * |
||
| 17 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 18 | * |
||
| 19 | * @throws \cs\ExitException |
||
| 20 | */ |
||
| 21 | function from_psr7 ($request) { |
||
| 22 | $this->from_psr7_server($request); |
||
| 23 | $this->from_psr7_query($request); |
||
| 24 | $this->from_psr7_data_and_files($request); |
||
| 25 | $this->init_route(); |
||
| 26 | } |
||
| 27 | /** |
||
| 28 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 29 | */ |
||
| 30 | protected function from_psr7_server ($request) { |
||
| 59 | /** |
||
| 60 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 61 | */ |
||
| 62 | protected function from_psr7_query ($request) { |
||
| 65 | /** |
||
| 66 | * @todo Implement custom stream wrapper for files and data in general in order to avoid data duplication |
||
| 67 | * |
||
| 68 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 69 | * |
||
| 70 | * @throws ExitException |
||
| 71 | */ |
||
| 72 | protected function from_psr7_data_and_files ($request) { |
||
| 73 | $data = []; |
||
| 74 | $data_stream = null; |
||
| 75 | $content_type = $this->header('content-type'); |
||
| 76 | $body_stream = $request->getBody(); |
||
| 77 | if (preg_match('#^application/([^+\s]+\+)?json#', $content_type)) { |
||
| 78 | $data = _json_decode((string)$body_stream) ?: []; |
||
| 79 | } elseif (strpos($content_type, 'application/x-www-form-urlencoded') === 0) { |
||
| 80 | @parse_str((string)$body_stream, $data); |
||
| 81 | } else { |
||
| 82 | try { |
||
| 83 | $position = $body_stream->tell(); |
||
| 84 | $body_stream->rewind(); |
||
| 85 | $data_stream = fopen('php://temp', 'w+b'); |
||
| 86 | while (!$body_stream->eof()) { |
||
| 87 | fwrite($data_stream, $body_stream->read(1024)); |
||
| 88 | } |
||
| 89 | $body_stream->seek($position); |
||
| 90 | } catch (Exception $e) { |
||
| 91 | // Do nothing |
||
| 92 | } |
||
| 93 | } |
||
| 94 | $this->init_data_and_files( |
||
|
1 ignored issue
–
show
|
|||
| 95 | $data, |
||
| 96 | $this->from_psr7_files_internal( |
||
| 97 | $request->getUploadedFiles() |
||
| 98 | ), |
||
| 99 | $data_stream |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | /** |
||
| 103 | * @param array|\Psr\Http\Message\UploadedFileInterface $files |
||
| 104 | * |
||
| 105 | * @return array|\Psr\Http\Message\UploadedFileInterface |
||
| 106 | */ |
||
| 107 | protected function from_psr7_files_internal ($files) { |
||
| 137 | } |
||
| 138 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.