Skip to content

Commit

Permalink
Added FlyResponses [closes #5]
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Mar 15, 2017
1 parent d57c074 commit 201d972
Show file tree
Hide file tree
Showing 13 changed files with 625 additions and 1 deletion.
138 changes: 138 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Content

- [LinkGenerator (LinkGeneratorExtesion)](#link-generator)
- [FlyResponse - send file/buffer on-the-fly](#flyresponse)

## Link Generator

Expand All @@ -13,3 +14,140 @@ URL addreses / links out of presenter scope. For example in mail templates.
extensions:
link: Contributte\Application\DI\LinkGeneratorExtesion
```
## FlyResponse
### FlyResponse
For common purpose and your custom solutions.
### FlyFileResponse
Special response for handling files on-the-fly.
## Adapters
### ProcessAdapter
Execute command over [popen](http://php.net/manual/en/function.popen.php).
```php
use Contributte\Application\Response\Fly\Adapter\ProcessAdapter;
use Contributte\Application\Response\Fly\FlyFileResponse;

// Compress current folder and send to response
$adapter = new ProcessAdapter('tar cf - ./ | gzip -c -f');
$response = new FlyFileResponse($adapter, 'folder.tgz');

$this->sendResponse($response);
```

### StdoutAdapter

Write to `php://output`.

```php
use Contributte\Application\Response\Fly\Adapter\StdoutAdapter;
use Contributte\Application\Response\Fly\Buffer\Buffer;
use Contributte\Application\Response\Fly\FlyFileResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse;

// Write to stdout over buffer class
$adapter = new StdoutAdapter(function(Buffer $buffer, IRequest $request, IResponse $response) {
// Modify headers
$response->setHeader(..);

// Write data
$buffer->write('Some data..');
});
$response = new FlyFileResponse($adapter, 'my.data');

$this->sendResponse($response);
```

### CallbackAdapter

```php
use Contributte\Application\Response\Fly\Adapter\CallbackAdapter;
use Contributte\Application\Response\Fly\Buffer\Buffer;
use Contributte\Application\Response\Fly\FlyFileResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse;

$adapter = new CallbackAdapter(function(IRequest $request, IResponse $response) use ($model) {
// Modify headers
$response->setHeader(..);

// Fetch topsecret data
$data = $this->facade->getData();
foreach ($data as $d) {
// Write or print data..
}
});
$response = new FlyFileResponse($adapter, 'my.data');

$this->sendResponse($response);
```

## Model

```php
final class BigOperationHandler
{

/** @var Facade */
private $facade;

/**
* @param Facade $facade
*/
public function __construct(Facade $facade)
{
$this->facade = $facade;
}

public function toFlyResponse()
{
$adapter = new CallbackAdapter(function (IRequest $request, IResponse $response) {
// Modify headers
$response->setHeader(..);

// Fetch topsecret data
$data = $this->facade->getData();
foreach ($data as $d) {
// Write or print data..
}
});

return new FlyFileResponse($adapter, 'file.ext');

// or
return new FlyResponse($adapter);
}
}

interface IBigOperationHandlerFactory
{

/**
* @return BigOperationHandler
*/
public function create();

}

final class MyPresenter extends Nette\Application\UI\Presenter
{

/** @var IBigOperationHandlerFactory @inject */
public $bigOperationHandlerFactory;

public function handleMagic()
{
$this->sendResponse(
$this->bigOperationHandlerFactory->create()->toFlyResponse()
);
}
}
```
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ composer require contributte/application

| State | Version | Branch | PHP |
|-------------|---------|----------|----------|
| development | `^0.1` | `master` | `>= 5.6` |
| development | `^0.2` | `master` | `>= 5.6` |

## Overview

- [LinkGenerator (LinkGeneratorExtension)](https://github.com/contributte/application/blob/master/.docs/README.md#link-generator)
- [FlyResponse - send file/buffer on-the-fly](https://github.com/contributte/application/blob/master/.docs/README.md#flyresponse)

---

Expand Down
18 changes: 18 additions & 0 deletions src/Response/Fly/Adapter/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Contributte\Application\Response\Fly\Adapter;

use Nette\Http\IRequest;
use Nette\Http\IResponse;

interface Adapter
{

/**
* @param IRequest $request
* @param IResponse $response
* @return void
*/
public function send(IRequest $request, IResponse $response);

}
32 changes: 32 additions & 0 deletions src/Response/Fly/Adapter/CallbackAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Contributte\Application\Response\Fly\Adapter;

use Nette\Http\IRequest;
use Nette\Http\IResponse;

class CallbackAdapter implements Adapter
{

/** @var callable */
private $callback;

/**
* @param callable $callback
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}

/**
* @param IRequest $request
* @param IResponse $response
* @return void
*/
public function send(IRequest $request, IResponse $response)
{
call_user_func_array($this->callback, [$request, $response]);
}

}
55 changes: 55 additions & 0 deletions src/Response/Fly/Adapter/ProcessAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Contributte\Application\Response\Fly\Adapter;

use Contributte\Application\Response\Fly\Buffer\ProcessBuffer;
use Nette\Http\IRequest;
use Nette\Http\IResponse;

class ProcessAdapter implements Adapter
{

/** @var string */
private $command;

/** @var string */
private $mode;

/** @var int */
private $buffersize;

/**
* @param string $command
* @param string $mode
* @param int $buffersize
*/
public function __construct($command, $mode = 'r', $buffersize = 8192)
{
$this->command = $command;
$this->mode = $mode;
$this->buffersize = $buffersize;
}

/**
* @param IRequest $request
* @param IResponse $response
* @return void
*/
public function send(IRequest $request, IResponse $response)
{
// Open file Buffer
$b = new ProcessBuffer($this->command, $this->mode);

while (!$b->eof()) {
// Read from Buffer
$output = $b->read($this->buffersize);

// Goes to ouput
echo $output;
}

// Close file Buffer
$b->close();
}

}
40 changes: 40 additions & 0 deletions src/Response/Fly/Adapter/StdoutAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Contributte\Application\Response\Fly\Adapter;

use Contributte\Application\Response\Fly\Buffer\FileBuffer;
use Nette\Http\IRequest;
use Nette\Http\IResponse;

class StdoutAdapter implements Adapter
{

/** @var callable */
private $callback;

/**
* @param callable $callback
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}

/**
* @param IRequest $request
* @param IResponse $response
* @return void
*/
public function send(IRequest $request, IResponse $response)
{
// Open file pointer
$b = new FileBuffer('php://output', 'w');

// Fire callback with Buffer, request and response
call_user_func_array($this->callback, [$b, $request, $response]);

// Close resource
$b->close();
}

}
32 changes: 32 additions & 0 deletions src/Response/Fly/Buffer/Buffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Contributte\Application\Response\Fly\Buffer;

interface Buffer
{

const BLOCK = 8196;

/**
* @param mixed $data
* @return void
*/
public function write($data);

/**
* @param int $size
* @return mixed
*/
public function read($size);

/**
* @return bool
*/
public function eof();

/**
* @return bool
*/
public function close();

}
Loading

0 comments on commit 201d972

Please sign in to comment.