| Total Complexity | 116 |
| Total Lines | 695 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PclzipZipInterface often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PclzipZipInterface, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 89 | class PclzipZipInterface |
||
| 90 | { |
||
| 91 | const SELECT_FILTER_PASS = 1; |
||
| 92 | const SELECT_FILTER_REFUSE = 0; |
||
| 93 | |||
| 94 | const AVERAGE_ZIP_COMPRESSION_RATIO = 2; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var UnifiedArchive |
||
| 98 | */ |
||
| 99 | private $archive; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * PclzipZipInterface constructor. |
||
| 103 | * |
||
| 104 | * @param UnifiedArchive $archive |
||
| 105 | */ |
||
| 106 | public function __construct(UnifiedArchive $archive) |
||
| 107 | { |
||
| 108 | $this->archive = $archive; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param $localname |
||
| 113 | * @param $filename |
||
| 114 | * |
||
| 115 | * @return object |
||
| 116 | */ |
||
| 117 | public function createFileHeader($localname, $filename) |
||
| 118 | { |
||
| 119 | return (object) array( |
||
| 120 | 'filename' => $filename, |
||
| 121 | 'stored_filename' => $localname, |
||
| 122 | 'size' => filesize($filename), |
||
| 123 | 'compressed_size' => ceil(filesize($filename) |
||
| 124 | / self::AVERAGE_ZIP_COMPRESSION_RATIO), |
||
| 125 | 'mtime' => filemtime($filename), |
||
| 126 | 'comment' => null, |
||
| 127 | 'folder' => is_dir($filename), |
||
| 128 | 'status' => 'ok', |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Creates a new archive |
||
| 134 | * Two ways of usage: |
||
| 135 | * <code>create($content, [$addDir, [$removeDir]])</code> |
||
| 136 | * <code>create($content, [... options ...]])</code> |
||
| 137 | */ |
||
| 138 | public function create($content) |
||
| 139 | { |
||
| 140 | if (is_array($content)) $paths_list = $content; |
||
| 141 | else $paths_list = explode(',', $content); |
||
| 142 | |||
| 143 | $options = $this->makeOptionsFromArguments(func_get_args()); |
||
| 144 | |||
| 145 | // filters initiation |
||
| 146 | $filters = $this->createFilters($options); |
||
| 147 | list($preAddCallback, $postAddCallback) = $this->extractCallbacks($options, PCLZIP_CB_PRE_ADD, PCLZIP_CB_POST_ADD); |
||
| 148 | |||
| 149 | if (!empty($comment = $this->buildComment($options, null))) |
||
| 150 | $this->archive->setComment($comment); |
||
| 151 | |||
| 152 | // scan filesystem for files list |
||
| 153 | return $this->addSnippets($paths_list, $filters, $preAddCallback, $postAddCallback); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $fileToAdd |
||
| 158 | * @param array $filters |
||
| 159 | * @param callable|null $preAddCallback |
||
| 160 | * @param callable|null $postAddCallback |
||
| 161 | * @return object |
||
| 162 | */ |
||
| 163 | private function addSnippet($fileToAdd, array $filters, callable $preAddCallback, callable $postAddCallback) |
||
| 164 | { |
||
| 165 | if (is_file($fileToAdd) || is_dir($fileToAdd)) { |
||
| 166 | // apply filters to a file |
||
| 167 | $localname = $fileToAdd; |
||
| 168 | $filename = $fileToAdd; |
||
| 169 | |||
| 170 | foreach ($filters as $filter) |
||
| 171 | call_user_func($filter, $localname, $filename); |
||
| 172 | |||
| 173 | $file_header = $this->createFileHeader($localname, $filename); |
||
| 174 | if (call_user_func($preAddCallback, $file_header) == 1) { |
||
| 175 | // |
||
| 176 | // Check for max length > 255 |
||
| 177 | // |
||
| 178 | if (strlen(basename($file_header->stored_filename)) > 255) |
||
| 179 | $file_header->status = 'filename_too_long'; |
||
| 180 | if (is_file($filename)) { |
||
| 181 | $this->archive->addFiles([ |
||
| 182 | $file_header->stored_filename => $file_header->filename, |
||
| 183 | ]); |
||
| 184 | } else if (is_dir($filename)) { |
||
| 185 | // $this->archive->addEmptyDir($file_header->stored_filename); |
||
| 186 | } |
||
| 187 | |||
| 188 | call_user_func($postAddCallback, $file_header); |
||
| 189 | |||
| 190 | } else { |
||
| 191 | // |
||
| 192 | // File was skipped |
||
| 193 | // |
||
| 194 | $file_header->status = 'skipped'; |
||
| 195 | } |
||
| 196 | |||
| 197 | return $file_header; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Lists archive content |
||
| 203 | * @throws Exceptions\NonExistentArchiveFileException |
||
| 204 | */ |
||
| 205 | public function listContent() |
||
| 206 | { |
||
| 207 | $filesList = []; |
||
| 208 | |||
| 209 | foreach ($this->archive->getFileNames() as $i => $fileName) { |
||
| 210 | $fileData = $this->archive->getFileData($fileName); |
||
| 211 | |||
| 212 | $filesList[] = (object) array( |
||
| 213 | 'filename' => $fileData->path, |
||
| 214 | 'stored_filename' => $fileData->path, |
||
| 215 | 'size' => $fileData->uncompressedSize, |
||
| 216 | 'compressed_size' => $fileData->compressedSize, |
||
| 217 | 'mtime' => $fileData->modificationTime, |
||
| 218 | 'comment' => $fileData->comment, |
||
| 219 | 'folder' => false/*in_array(substr($statIndex['name'], -1), |
||
| 220 | array('/', '\\'))*/, |
||
| 221 | 'index' => $i, |
||
| 222 | 'status' => 'ok', |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | |||
| 226 | return $filesList; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Extracts files |
||
| 231 | * Two ways of usage: |
||
| 232 | * <code>extract([$extractPath, [$removePath]])</code> |
||
| 233 | * <code>extract([... options ...]])</code> |
||
| 234 | */ |
||
| 235 | public function extract() |
||
| 236 | { |
||
| 237 | $options = func_get_args(); |
||
| 238 | array_shift($options); |
||
| 239 | |||
| 240 | // parse options |
||
| 241 | if (isset($options[0]) && is_string($options[0])) { |
||
| 242 | $options[PCLZIP_OPT_PATH] = $options[0]; |
||
| 243 | if (isset($options[1]) && is_string($options[1])) { |
||
| 244 | $options[PCLZIP_OPT_REMOVE_PATH] = $options[1]; |
||
| 245 | } |
||
| 246 | } else { |
||
| 247 | $options = $this->makeKeyValueArrayFromList($options); |
||
| 248 | } |
||
| 249 | |||
| 250 | // filters initiation |
||
| 251 | if (isset($options[PCLZIP_OPT_PATH])) |
||
| 252 | $extractPath = rtrim($options[PCLZIP_OPT_PATH], '/'); |
||
| 253 | else $extractPath = rtrim(getcwd(), '/'); |
||
| 254 | |||
| 255 | $filters = $this->createFilters($options); |
||
| 256 | list($preExtractCallback, $postExtractCallback) = $this->extractCallbacks($options, PCLZIP_CB_PRE_EXTRACT, PCLZIP_CB_POST_EXTRACT); |
||
| 257 | $selectFilter = $this->createSelector($options); |
||
| 258 | |||
| 259 | if (isset($options[PCLZIP_OPT_EXTRACT_AS_STRING])) |
||
| 260 | $anotherOutputFormat = PCLZIP_OPT_EXTRACT_AS_STRING; |
||
| 261 | else if (isset($options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) |
||
| 262 | $anotherOutputFormat = PCLZIP_OPT_EXTRACT_IN_OUTPUT; |
||
| 263 | else $anotherOutputFormat = false; |
||
| 264 | |||
| 265 | if (isset($options[PCLZIP_OPT_REPLACE_NEWER])) |
||
| 266 | $doNotReplaceNewer = false; |
||
| 267 | else $doNotReplaceNewer = true; |
||
| 268 | |||
| 269 | if (isset($options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) |
||
| 270 | $restrictExtractDir = $options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION]; |
||
| 271 | else $restrictExtractDir = false; |
||
| 272 | |||
| 273 | $report = array(); |
||
| 274 | foreach ($this->listContent() as $file_header) { |
||
| 275 | // add file information to report |
||
| 276 | $report[] = $file_header; |
||
| 277 | // refuse by select rule |
||
| 278 | if (call_user_func($selectFilter, $file_header->stored_filename, |
||
| 279 | $file_header->filename, $file_header->index) |
||
| 280 | === self::SELECT_FILTER_REFUSE) { |
||
| 281 | // |
||
| 282 | // I don't know need to remain this file in report or not, |
||
| 283 | // but for now I remove |
||
| 284 | array_pop($report); |
||
| 285 | // $file_header->status = 'filtered'; |
||
| 286 | // |
||
| 287 | continue; |
||
| 288 | } |
||
| 289 | |||
| 290 | // |
||
| 291 | // add extract path in case of extraction |
||
| 292 | // for some reason need to do it before call pre extract callback |
||
| 293 | // (pclzip.lib.php v2.8.2, line 3670) |
||
| 294 | // so I decided to do it here too |
||
| 295 | // |
||
| 296 | if ($anotherOutputFormat === false) { |
||
| 297 | $file_header->filename = realpath($extractPath.'/'. |
||
| 298 | $file_header->filename); |
||
| 299 | // |
||
| 300 | // check for path correlation with restricted path |
||
| 301 | // |
||
| 302 | if ($restrictExtractDir !== false) { |
||
| 303 | $filename = $file_header->filename; |
||
| 304 | $restrictedDir = realpath($restrictExtractDir); |
||
| 305 | if (strncasecmp($restrictedDir, $filename, |
||
| 306 | strlen($restrictedDir)) !== 0) { |
||
| 307 | // refuse file extraction |
||
| 308 | $file_header->status = 'filtered'; |
||
| 309 | continue; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | // apply pre extract callback |
||
| 315 | $callback_result = call_user_func($preExtractCallback, |
||
| 316 | $file_header); |
||
| 317 | if ($callback_result == 1) { |
||
| 318 | // go on ... |
||
| 319 | } elseif ($callback_result == 0) { |
||
| 320 | // skip current file |
||
| 321 | $file_header->status = 'skipped'; |
||
| 322 | continue; |
||
| 323 | } elseif ($callback_result == 2) { |
||
| 324 | // skip & stop extraction |
||
| 325 | $file_header->status = 'aborted'; |
||
| 326 | break; |
||
| 327 | } |
||
| 328 | |||
| 329 | // return content |
||
| 330 | if ($anotherOutputFormat == PCLZIP_OPT_EXTRACT_AS_STRING) { |
||
| 331 | $file_header->content |
||
| 332 | = $this->archive->getFileContent($file_header->stored_filename); |
||
| 333 | } |
||
| 334 | // echo content |
||
| 335 | else if ($anotherOutputFormat == PCLZIP_OPT_EXTRACT_IN_OUTPUT) { |
||
| 336 | echo $this->archive->getFileContent($file_header->stored_filename); |
||
| 337 | } |
||
| 338 | // extract content |
||
| 339 | else if ($anotherOutputFormat === false) { |
||
| 340 | // apply path filters |
||
| 341 | foreach ($filters as $filter) call_user_func($filter, |
||
| 342 | $file_header->stored_filename, $file_header->filename); |
||
| 343 | // dir extraction process |
||
| 344 | if ($file_header->folder) { |
||
| 345 | // if dir doesn't exist |
||
| 346 | if (!is_dir($file_header->filename)) { |
||
| 347 | // try to create folder |
||
| 348 | if (!mkdir($file_header)) { |
||
| 349 | $file_header->status = 'path_creation_fail'; |
||
| 350 | continue; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | // file extraction process |
||
| 355 | else { |
||
| 356 | // check if path is already taken by a folder |
||
| 357 | if (is_dir($file_header->filename)) { |
||
| 358 | $file_header->status = 'already_a_directory'; |
||
| 359 | continue; |
||
| 360 | } |
||
| 361 | // check if file path is not writable |
||
| 362 | if (!is_writable($file_header->filename)) { |
||
| 363 | $file_header->status = 'write_protected'; |
||
| 364 | continue; |
||
| 365 | } |
||
| 366 | // check if file exists and it's newer |
||
| 367 | if (is_file($file_header->filename)) { |
||
| 368 | if (filemtime($file_header->filename) |
||
| 369 | > $file_header->mtime) { |
||
| 370 | // skip extraction if option EXTRACT_NEWER isn't set |
||
| 371 | if ($doNotReplaceNewer) { |
||
| 372 | $file_header->status = 'newer_exist'; |
||
| 373 | continue; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 | $directory = dirname($file_header->filename); |
||
| 378 | // check if running process can not create extraction folder |
||
| 379 | if (!is_dir($directory)) { |
||
| 380 | if (!mkdir($directory)) { |
||
| 381 | $file_header->status = 'path_creation_fail'; |
||
| 382 | continue; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | // extraction |
||
| 386 | if (copy('zip://'.$this->archive->filename."#" |
||
|
|
|||
| 387 | .$file_header->stored_filename |
||
| 388 | , $file_header->filename)) { |
||
| 389 | // ok |
||
| 390 | } |
||
| 391 | // extraction fails |
||
| 392 | else { |
||
| 393 | $file_header->status = 'write_error'; |
||
| 394 | continue; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | // apply post extract callback |
||
| 400 | $callback_result = call_user_func($postExtractCallback, |
||
| 401 | $file_header); |
||
| 402 | if ($callback_result == 1) { |
||
| 403 | // go on |
||
| 404 | } elseif ($callback_result == 2) { |
||
| 405 | // skip & stop extraction |
||
| 406 | break; |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | return $report; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Reads properties of archive |
||
| 415 | */ |
||
| 416 | public function properties() |
||
| 417 | { |
||
| 418 | return [ |
||
| 419 | 'nb' => $this->archive->countFiles(), |
||
| 420 | 'comment' => $this->archive->getComment(), |
||
| 421 | 'status' => 'OK', |
||
| 422 | ]; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Adds files in archive |
||
| 427 | * <code>add($content, [$addDir, [$removeDir]])</code> |
||
| 428 | * <code>add($content, [ ... options ... ])</code> |
||
| 429 | */ |
||
| 430 | public function add($content) |
||
| 431 | { |
||
| 432 | if (is_array($content)) $paths_list = $content; |
||
| 433 | else $paths_list = explode(',', $content); |
||
| 434 | |||
| 435 | $options = $this->makeOptionsFromArguments(func_get_args()); |
||
| 436 | $filters = $this->createFilters($options); |
||
| 437 | list($preAddCallback, $postAddCallback) = $this->extractCallbacks($options, PCLZIP_CB_PRE_ADD, PCLZIP_CB_POST_ADD); |
||
| 438 | |||
| 439 | if (!empty($comment = $this->buildComment($options, $this->archive->getComment()))) |
||
| 440 | $this->archive->setComment($comment); |
||
| 441 | |||
| 442 | // scan filesystem for files list |
||
| 443 | return $this->addSnippets($paths_list, $filters, $preAddCallback, $postAddCallback); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Removes files from archive |
||
| 448 | * Usage: |
||
| 449 | * <code>delete([... options ...])</code> |
||
| 450 | */ |
||
| 451 | public function delete() |
||
| 452 | { |
||
| 453 | $options = $this->makeKeyValueArrayFromList(func_get_args()); |
||
| 454 | $selectFilter = $this->createSelector($options); |
||
| 455 | |||
| 456 | $report = []; |
||
| 457 | foreach ($this->listContent() as $file_header) { |
||
| 458 | // select by select rule |
||
| 459 | if (call_user_func($selectFilter, $file_header->stored_filename, |
||
| 460 | $file_header->filename, $file_header->index) |
||
| 461 | === self::SELECT_FILTER_REFUSE) { |
||
| 462 | // delete file from archive |
||
| 463 | if ($this->archive->deleteFiles($file_header->stored_filename)) { |
||
| 464 | // ok |
||
| 465 | continue; |
||
| 466 | } |
||
| 467 | // deletion fails |
||
| 468 | else { |
||
| 469 | return 0; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | // unselected file add in report |
||
| 473 | $report[] = $file_header; |
||
| 474 | } |
||
| 475 | |||
| 476 | return $report; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Merges given archive into current archive |
||
| 481 | * Two ways of usage: |
||
| 482 | * <code>merge($filename)</code> |
||
| 483 | * <code>merge(UnifiedArchive $unifiedArchiveInstance)</code> |
||
| 484 | * This implementation is more intelligent than original' one. |
||
| 485 | */ |
||
| 486 | public function merge($a) |
||
| 487 | { |
||
| 488 | // filename |
||
| 489 | if (is_string($a)) { |
||
| 490 | if ($a = UnifiedArchive::open($a) !== null) { |
||
| 491 | // ok |
||
| 492 | } else { |
||
| 493 | // // unsupported type of archive |
||
| 494 | return 0; |
||
| 495 | } |
||
| 496 | } |
||
| 497 | // UnifiedArchive instance |
||
| 498 | else if ($a instanceof UnifiedArchive) { |
||
| 499 | // go on |
||
| 500 | } |
||
| 501 | // invalid argument |
||
| 502 | else { |
||
| 503 | return 0; |
||
| 504 | } |
||
| 505 | |||
| 506 | $tempDir = tempnam(PCLZIP_TEMPORARY_DIR, 'merging'); |
||
| 507 | if (file_exists($tempDir)) unlink($tempDir); |
||
| 508 | if (!mkdir($tempDir)) return 0; |
||
| 509 | |||
| 510 | // go through archive content list and copy all files |
||
| 511 | foreach ($a->getFileNames() as $filename) { |
||
| 512 | // dir merging process |
||
| 513 | if (in_array(substr($filename, -1), array('/', '\\'))) { |
||
| 514 | $this->archive->addEmptyDir(rtrim($filename, '/\\')); |
||
| 515 | } |
||
| 516 | // file merging process |
||
| 517 | else { |
||
| 518 | // extract file in temporary dir |
||
| 519 | if ($a->extractNode($tempDir, '/'.$filename)) { |
||
| 520 | // go on |
||
| 521 | } else { |
||
| 522 | // extraction fails |
||
| 523 | return 0; |
||
| 524 | } |
||
| 525 | // add file in archive |
||
| 526 | if ($this->archive->addFile($tempDir.'/'.$filename, |
||
| 527 | $filename)) { |
||
| 528 | // ok |
||
| 529 | } else { |
||
| 530 | return 0; |
||
| 531 | } |
||
| 532 | } |
||
| 533 | } |
||
| 534 | |||
| 535 | call_user_func(function ($directory) { |
||
| 536 | foreach (glob($directory.'/*') as $f) { |
||
| 537 | if (is_dir($f)) call_user_func(__FUNCTION__, $f); |
||
| 538 | else unlink($f); |
||
| 539 | } |
||
| 540 | }, $tempDir); |
||
| 541 | |||
| 542 | return 1; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Duplicates archive |
||
| 547 | * @param $clone_filename |
||
| 548 | * @return int |
||
| 549 | */ |
||
| 550 | public function duplicate($clone_filename) |
||
| 551 | { |
||
| 552 | return copy($this->archive->filename, $clone_filename) ? 1 : 0; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param array $options |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | public function createFilters(array $options) |
||
| 560 | { |
||
| 561 | $filters = array(); |
||
| 562 | if (isset($options[PCLZIP_OPT_REMOVE_PATH]) |
||
| 563 | && !isset($options[PCLZIP_OPT_REMOVE_ALL_PATH])) |
||
| 564 | $filters[] = function (&$key, &$value) use ($options) { |
||
| 565 | $key = str_replace($key, null, $key); |
||
| 566 | }; |
||
| 567 | if (isset($options[PCLZIP_OPT_REMOVE_ALL_PATH])) |
||
| 568 | $filters[] = function (&$key, &$value) { |
||
| 569 | $key = basename($key); |
||
| 570 | }; |
||
| 571 | if (isset($options[PCLZIP_OPT_ADD_PATH])) |
||
| 572 | $filters[] = function (&$key, &$value) use ($options) { |
||
| 573 | $key = rtrim($options[PCLZIP_OPT_ADD_PATH], '/') . '/' . |
||
| 574 | ltrim($key, '/'); |
||
| 575 | }; |
||
| 576 | return $filters; |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param array $options |
||
| 581 | * @param string $preCallbackConst |
||
| 582 | * @param string $postCallbackConst |
||
| 583 | * @return callable[]|\Closure[] |
||
| 584 | */ |
||
| 585 | private function extractCallbacks(array $options, $preCallbackConst, $postCallbackConst) |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param array $options |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | private function makeKeyValueArrayFromList(array $options) |
||
| 603 | { |
||
| 604 | return array_combine( |
||
| 605 | array_filter($options, function ($v) {return (bool) $v&2;}), |
||
| 606 | array_filter($options, function ($v) {return (bool) ($v-1)&2;}) |
||
| 607 | ); |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param array $pathsList |
||
| 612 | * @param array $filters |
||
| 613 | * @param callable $preAddCallback |
||
| 614 | * @param callable $postAddCallback |
||
| 615 | * @return array |
||
| 616 | */ |
||
| 617 | public function addSnippets(array $pathsList, array $filters, callable $preAddCallback, callable $postAddCallback) |
||
| 618 | { |
||
| 619 | $report = []; |
||
| 620 | |||
| 621 | foreach ($pathsList as $file_to_add) { |
||
| 622 | $report[] = $this->addSnippet($file_to_add, $filters, |
||
| 623 | $preAddCallback, $postAddCallback); |
||
| 624 | |||
| 625 | // additional dir contents |
||
| 626 | if (is_dir($file_to_add)) { |
||
| 627 | $directory_contents = new \RecursiveIteratorIterator( |
||
| 628 | new \RecursiveDirectoryIterator( |
||
| 629 | $file_to_add, \RecursiveDirectoryIterator::SKIP_DOTS), |
||
| 630 | RecursiveIteratorIterator::SELF_FIRST); |
||
| 631 | foreach ($directory_contents as $file_to_add) { |
||
| 632 | $report[] = $this->addSnippet($file_to_add, $filters, |
||
| 633 | $preAddCallback, $postAddCallback); |
||
| 634 | } |
||
| 635 | } |
||
| 636 | } |
||
| 637 | return $report; |
||
| 638 | } |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param array $indexes |
||
| 642 | * @return \Closure |
||
| 643 | */ |
||
| 644 | protected function createByIndexSelector(array $indexes) |
||
| 645 | { |
||
| 646 | $allowedIndexes = array(); |
||
| 647 | foreach ($indexes as $rule) { |
||
| 648 | $parts = explode('-', $rule); |
||
| 649 | if (count($parts) == 1) $allowedIndexes[] = $rule; |
||
| 650 | else $allowedIndexes = array_merge( |
||
| 651 | range($parts[0], $parts[1]), $allowedIndexes); |
||
| 652 | } |
||
| 653 | |||
| 654 | return function ($key, $value, $index) use ($allowedIndexes) { |
||
| 655 | return in_array($index, $allowedIndexes) |
||
| 656 | ? self::SELECT_FILTER_PASS |
||
| 657 | : self::SELECT_FILTER_REFUSE; |
||
| 658 | }; |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param string|array $names |
||
| 663 | * @return \Closure |
||
| 664 | */ |
||
| 665 | protected function createByNameSelector($names) |
||
| 666 | { |
||
| 667 | $allowedNames = is_array($names) |
||
| 668 | ? $names |
||
| 669 | : explode(',', $names); |
||
| 670 | |||
| 671 | return function ($key, $value) use ($allowedNames) { |
||
| 672 | foreach ($allowedNames as $name) { |
||
| 673 | // select directory with nested files |
||
| 674 | if (in_array(substr($name, -1), ['/', '\\'])) { |
||
| 675 | if (strncasecmp($name, $key, strlen($name)) === 0) { |
||
| 676 | // that's a file inside a dir or that dir |
||
| 677 | return self::SELECT_FILTER_PASS; |
||
| 678 | } |
||
| 679 | } else { |
||
| 680 | // select exact name only |
||
| 681 | if (strcasecmp($name, $key) === 0) { |
||
| 682 | // that's a file with this name |
||
| 683 | return self::SELECT_FILTER_PASS; |
||
| 684 | } |
||
| 685 | } |
||
| 686 | } |
||
| 687 | |||
| 688 | // that file is not in allowed list |
||
| 689 | return self::SELECT_FILTER_REFUSE; |
||
| 690 | }; |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @param string $regex |
||
| 695 | * @return \Closure |
||
| 696 | */ |
||
| 697 | protected function createByEregSelector($regex) |
||
| 698 | { |
||
| 699 | return function ($key, $value) use ($regex) { |
||
| 700 | return (ereg($regex, $key) !== false) |
||
| 701 | ? self::SELECT_FILTER_PASS |
||
| 702 | : self::SELECT_FILTER_REFUSE; |
||
| 703 | }; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param $regex |
||
| 708 | * @return \Closure |
||
| 709 | */ |
||
| 710 | protected function createByPregSelector($regex) |
||
| 711 | { |
||
| 712 | return function ($key, $value) use ($regex) { |
||
| 713 | return preg_match($regex, $key) |
||
| 714 | ? self::SELECT_FILTER_PASS |
||
| 715 | : self::SELECT_FILTER_REFUSE; |
||
| 716 | }; |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @param array $options |
||
| 721 | * @return callable |
||
| 722 | */ |
||
| 723 | protected function createSelector(array $options) |
||
| 724 | { |
||
| 725 | // exact matching |
||
| 726 | if (isset($options[PCLZIP_OPT_BY_NAME])) |
||
| 727 | $selectFilter = $this->createByNameSelector($options[PCLZIP_OPT_BY_NAME]); |
||
| 728 | // <ereg> rule |
||
| 729 | else if (isset($options[PCLZIP_OPT_BY_EREG]) && function_exists('ereg')) |
||
| 730 | $selectFilter = $this->createByEregSelector($options[PCLZIP_OPT_BY_EREG]); |
||
| 731 | // <preg_match> rule |
||
| 732 | else if (isset($options[PCLZIP_OPT_BY_PREG])) |
||
| 733 | $selectFilter = $this->createByPregSelector($options[PCLZIP_OPT_BY_PREG]); |
||
| 734 | // index rule |
||
| 735 | else if (isset($options[PCLZIP_OPT_BY_INDEX])) |
||
| 736 | $selectFilter = $this->createByIndexSelector($options[PCLZIP_OPT_BY_INDEX]); |
||
| 737 | // no rule |
||
| 738 | else |
||
| 739 | $selectFilter = function () { |
||
| 740 | return self::SELECT_FILTER_PASS; |
||
| 741 | }; |
||
| 742 | return $selectFilter; |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @param array $args |
||
| 747 | * @return array |
||
| 748 | */ |
||
| 749 | protected function makeOptionsFromArguments(array $args) |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @param array $options |
||
| 770 | * @param string|null $currentComment |
||
| 771 | * @return mixed|string|null |
||
| 772 | */ |
||
| 773 | protected function buildComment(array $options, $currentComment) |
||
| 774 | { |
||
| 775 | $comment = null; |
||
| 776 | if (isset($options[PCLZIP_OPT_COMMENT])) |
||
| 777 | $comment = $options[PCLZIP_OPT_COMMENT]; |
||
| 778 | else if (isset($options[PCLZIP_OPT_ADD_COMMENT])) {; |
||
| 779 | $comment = $currentComment . $options[PCLZIP_OPT_ADD_COMMENT]; |
||
| 780 | } else if (isset($options[PCLZIP_OPT_PREPEND_COMMENT])) { |
||
| 784 | } |
||
| 785 | } |
||
| 786 |