Skip to content

Commit a11c636

Browse files
committed
added method outputAsSymfonyResponse, rename method outputAsResponse to outputAsPsr7Response
1 parent e8418d5 commit a11c636

File tree

6 files changed

+274
-79
lines changed

6 files changed

+274
-79
lines changed

.phpstorm.meta.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
expectedArguments(\PhpZip\ZipFile::outputAsResponse(), 2, argumentsSet("zip_mime_types"));
6666
expectedArguments(\PhpZip\ZipFile::outputAsResponse(), 3, argumentsSet("bool"));
6767

68+
expectedArguments(\PhpZip\ZipFile::outputAsPsr7Response(), 2, argumentsSet("zip_mime_types"));
69+
expectedArguments(\PhpZip\ZipFile::outputAsPsr7Response(), 3, argumentsSet("bool"));
70+
71+
expectedArguments(\PhpZip\ZipFile::outputAsSymfonyResponse(), 1, argumentsSet("zip_mime_types"));
72+
expectedArguments(\PhpZip\ZipFile::outputAsSymfonyResponse(), 2, argumentsSet("bool"));
73+
6874
registerArgumentsSet(
6975
'dos_charset',
7076
\PhpZip\Constants\DosCodePage::CP_LATIN_US,

README.RU.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ finally{
144144
- [ZipFile::openFromString](#zipfileopenfromstring) - открывает ZIP-архив из строки.
145145
- [ZipFile::openFromStream](#zipfileopenfromstream) - открывает ZIP-архив из потока.
146146
- [ZipFile::outputAsAttachment](#zipfileoutputasattachment) - выводит ZIP-архив в браузер.
147-
- [ZipFile::outputAsResponse](#zipfileoutputasresponse) - выводит ZIP-архив, как Response PSR-7.
147+
- [ZipFile::outputAsPsr7Response](#zipfileoutputaspsr7response) - выводит ZIP-архив, как PSR-7 Response.
148+
- [ZipFile::outputAsSymfonyResponse](#zipfileoutputassymfonyresponse) - выводит ZIP-архив, как Symfony Response.
148149
- [ZipFile::outputAsString](#zipfileoutputasstring) - выводит ZIP-архив в виде строки.
149150
- [ZipFile::rename](#zipfilerename) - переименовывает запись по имени.
150151
- [ZipFile::rewrite](#zipfilerewrite) - сохраняет изменения и заново открывает изменившийся архив.
@@ -753,28 +754,57 @@ $zipFile->outputAsAttachment($outputFilename);
753754
$mimeType = 'application/zip';
754755
$zipFile->outputAsAttachment($outputFilename, $mimeType);
755756
```
756-
##### ZipFile::outputAsResponse
757-
Выводит ZIP-архив, как Response [PSR-7](http://www.php-fig.org/psr/psr-7/).
757+
##### ZipFile::outputAsPsr7Response
758+
Выводит ZIP-архив, как [PSR-7 Response](http://www.php-fig.org/psr/psr-7/).
758759

759760
Метод вывода может использоваться в любом PSR-7 совместимом фреймворке.
760761
```php
761762
// $response = ....; // instance Psr\Http\Message\ResponseInterface
762-
$zipFile->outputAsResponse($response, $outputFilename);
763+
$zipFile->outputAsPsr7Response($response, $outputFilename);
763764
```
764765
Можно установить MIME-тип:
765766
```php
766767
$mimeType = 'application/zip';
767-
$zipFile->outputAsResponse($response, $outputFilename, $mimeType);
768+
$zipFile->outputAsPsr7Response($response, $outputFilename, $mimeType);
768769
```
769-
Пример для Slim Framework:
770+
##### ZipFile::outputAsSymfonyResponse
771+
Выводит ZIP-архив, как [Symfony Response](https://symfony.com/doc/current/components/http_foundation.html#response).
772+
773+
Метод вывода можно использовать в фреймворке Symfony.
770774
```php
771-
$app = new \Slim\App;
772-
$app->get('/download', function ($req, $res, $args) {
773-
$zipFile = new \PhpZip\ZipFile();
774-
$zipFile['file.txt'] = 'content';
775-
return $zipFile->outputAsResponse($res, 'file.zip');
776-
});
777-
$app->run();
775+
$response = $zipFile->outputAsSymfonyResponse($outputFilename);
776+
```
777+
Вы можете установить Mime-Type:
778+
```php
779+
$mimeType = 'application/zip';
780+
$response = $zipFile->outputAsSymfonyResponse($outputFilename, $mimeType);
781+
```
782+
Пример использования в Symfony Controller:
783+
```php
784+
<?php
785+
786+
namespace App\Controller;
787+
788+
use PhpZip\ZipFile;
789+
use Symfony\Component\HttpFoundation\Response;
790+
use Symfony\Component\Routing\Annotation\Route;
791+
792+
class DownloadZipController
793+
{
794+
/**
795+
* @Route("/downloads/{id}")
796+
*
797+
* @throws \PhpZip\Exception\ZipException
798+
*/
799+
public function __invoke(string $id): Response
800+
{
801+
$zipFile = new ZipFile();
802+
$zipFile['file'] = 'contents';
803+
804+
$outputFilename = $id . '.zip';
805+
return $zipFile->outputAsSymfonyResponse($outputFilename);
806+
}
807+
}
778808
```
779809
##### ZipFile::rewrite
780810
Сохраняет изменения и заново открывает изменившийся архив.

README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ Other examples can be found in the `tests/` folder
149149
- [ZipFile::openFromString](#zipfileopenfromstring) - opens a zip-archive from a string.
150150
- [ZipFile::openFromStream](#zipfileopenfromstream) - opens a zip-archive from the stream.
151151
- [ZipFile::outputAsAttachment](#zipfileoutputasattachment) - outputs a ZIP-archive to the browser.
152-
- [ZipFile::outputAsResponse](#zipfileoutputasresponse) - outputs a ZIP-archive as PSR-7 Response.
152+
- [ZipFile::outputAsPsr7Response](#zipfileoutputaspsr7response) - outputs a ZIP-archive as PSR-7 Response.
153+
- [ZipFile::outputAsSymfonyResponse](#zipfileoutputaspsr7response) - outputs a ZIP-archive as Symfony Response.
153154
- [ZipFile::outputAsString](#zipfileoutputasstring) - outputs a ZIP-archive as string.
154155
- [ZipFile::rename](#zipfilerename) - renames an entry defined by its name.
155156
- [ZipFile::rewrite](#zipfilerewrite) - save changes and re-open the changed archive.
@@ -782,18 +783,57 @@ You can set the Mime-Type:
782783
$mimeType = 'application/zip';
783784
$zipFile->outputAsAttachment($outputFilename, $mimeType);
784785
```
785-
##### ZipFile::outputAsResponse
786+
##### ZipFile::outputAsPsr7Response
786787
Outputs a ZIP-archive as [PSR-7 Response](http://www.php-fig.org/psr/psr-7/).
787788

788789
The output method can be used in any PSR-7 compatible framework.
789790
```php
790791
// $response = ....; // instance Psr\Http\Message\ResponseInterface
791-
$zipFile->outputAsResponse($response, $outputFilename);
792+
$zipFile->outputAsPsr7Response($response, $outputFilename);
792793
```
793794
You can set the Mime-Type:
794795
```php
795796
$mimeType = 'application/zip';
796-
$zipFile->outputAsResponse($response, $outputFilename, $mimeType);
797+
$zipFile->outputAsPsr7Response($response, $outputFilename, $mimeType);
798+
```
799+
##### ZipFile::outputAsSymfonyResponse
800+
Outputs a ZIP-archive as [Symfony Response](https://symfony.com/doc/current/components/http_foundation.html#response).
801+
802+
The output method can be used in Symfony framework.
803+
```php
804+
$response = $zipFile->outputAsSymfonyResponse($outputFilename);
805+
```
806+
You can set the Mime-Type:
807+
```php
808+
$mimeType = 'application/zip';
809+
$response = $zipFile->outputAsSymfonyResponse($outputFilename, $mimeType);
810+
```
811+
Example use in Symfony Controller:
812+
```php
813+
<?php
814+
815+
namespace App\Controller;
816+
817+
use PhpZip\ZipFile;
818+
use Symfony\Component\HttpFoundation\Response;
819+
use Symfony\Component\Routing\Annotation\Route;
820+
821+
class DownloadZipController
822+
{
823+
/**
824+
* @Route("/downloads/{id}")
825+
*
826+
* @throws \PhpZip\Exception\ZipException
827+
*/
828+
public function __invoke(string $id): Response
829+
{
830+
$zipFile = new ZipFile();
831+
$zipFile['file'] = 'contents';
832+
833+
$outputFilename = $id . '.zip';
834+
return $zipFile->outputAsSymfonyResponse($outputFilename);
835+
}
836+
}
797837
```
798838
##### ZipFile::rewrite
799839
Save changes and re-open the changed archive.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"phpunit/phpunit": "^9",
3737
"symfony/var-dumper": "^5.0",
3838
"friendsofphp/php-cs-fixer": "^2.18",
39-
"vimeo/psalm": "^4.6"
39+
"vimeo/psalm": "^4.6",
40+
"symfony/http-foundation": "^5.2"
4041
},
4142
"autoload": {
4243
"psr-4": {

0 commit comments

Comments
 (0)