4 * @see https://github.com/laminas/laminas-progressbar for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-progressbar/blob/master/COPYRIGHT.md
6 * @license https://github.com/laminas/laminas-progressbar/blob/master/LICENSE.md New BSD License
9 namespace Laminas\ProgressBar\Upload;
11 use Laminas\ProgressBar\Adapter\AbstractAdapter as AbstractProgressAdapter;
12 use Laminas\ProgressBar\Exception;
13 use Laminas\ProgressBar\ProgressBar;
14 use Laminas\Stdlib\ArrayUtils;
18 * Abstract class for Upload Progress Handlers
20 abstract class AbstractUploadHandler implements UploadHandlerInterface
25 protected $sessionNamespace = 'Laminas\ProgressBar\Upload\AbstractUploadHandler';
28 * @var AbstractProgressAdapter|ProgressBar
30 protected $progressAdapter;
33 * @param array|Traversable $options Optional options
34 * @throws Exception\InvalidArgumentException
36 public function __construct($options = [])
38 if (! empty($options)) {
39 $this->setOptions($options);
44 * Set options for a upload handler. Accepted options are:
45 * - session_namespace: session namespace for upload progress
46 * - progress_adapter: progressbar adapter to use for updating progress
48 * @param array|Traversable $options
49 * @return AbstractUploadHandler
50 * @throws Exception\InvalidArgumentException
52 public function setOptions($options)
54 if ($options instanceof Traversable) {
55 $options = ArrayUtils::iteratorToArray($options);
56 } elseif (! is_array($options)) {
57 throw new Exception\InvalidArgumentException(
58 'The options parameter must be an array or a Traversable'
62 if (isset($options['session_namespace'])) {
63 $this->setSessionNamespace($options['session_namespace']);
65 if (isset($options['progress_adapter'])) {
66 $this->setProgressAdapter($options['progress_adapter']);
73 * @param string $sessionNamespace
74 * @return AbstractUploadHandler|UploadHandlerInterface
76 public function setSessionNamespace($sessionNamespace)
78 $this->sessionNamespace = $sessionNamespace;
85 public function getSessionNamespace()
87 return $this->sessionNamespace;
91 * @param AbstractProgressAdapter|ProgressBar $progressAdapter
92 * @return AbstractUploadHandler|UploadHandlerInterface
94 public function setProgressAdapter($progressAdapter)
96 $this->progressAdapter = $progressAdapter;
101 * @return AbstractProgressAdapter|ProgressBar
103 public function getProgressAdapter()
105 return $this->progressAdapter;
112 public function getProgress($id)
118 'message' => 'No upload in progress',
125 $newStatus = $this->getUploadProgress($id);
126 if (false === $newStatus) {
129 $status = $newStatus;
130 if ('' === $status['message']) {
131 $status['message'] = $this->toByteString($status['current']) .
132 " - " . $this->toByteString($status['total']);
136 $adapter = $this->getProgressAdapter();
137 if (isset($adapter)) {
138 if ($adapter instanceof AbstractProgressAdapter) {
139 $adapter = new ProgressBar($adapter, 0, $status['total'], $this->getSessionNamespace());
140 $this->setProgressAdapter($adapter);
143 if (! $adapter instanceof ProgressBar) {
144 throw new Exception\RuntimeException('Unknown Adapter type given');
147 if ($status['done']) {
150 $adapter->update($status['current'], $status['message']);
161 abstract protected function getUploadProgress($id);
164 * Returns the formatted size
169 protected function toByteString($size)
171 $sizes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
172 for ($i = 0; $size >= 1024 && $i < 9; $i++) {
176 return round($size, 2) . $sizes[$i];