ArchiveCreator.php 1.71 KB
<?php
/**
 * Created by PhpStorm.
 * User: Tsurkanov
 * Date: 03.11.2015
 * Time: 14:51
 */

namespace common\components\archives;


class ArchiveCreator {

  protected  $handleExtension = ['rar', 'zip'];

    public  function create( $file, $ext ){
//        if ( $this->isHandleableExtension( $ext )) {
//            $arh_class = ucfirst( $ext ) . 'ArchiveReader';
//            if ( class_exists( $arh_class ) ) {
//
//                $arh_reader =  new $arh_class();
//
//                if ($arh_reader instanceof ArchiveReader ) {
//                    $arh_reader->open($file);
//                    return $arh_reader;
//                }
//
//            }
//
//        }
        if ( $ext  = 'zip' ) {
            $arh_reader =  new ZipArchiveReader();
        }else{
            $arh_reader =  new RarArchiveReader();
        }

        $arh_reader->open($file);
        return $arh_reader;
        // не найден подходящий обработчик
        throw new \Exception( "Для расширения {$ext} не найден подходящий распаковщик" );
    }

    protected  function isHandleableExtension( $ext ){

       // $this->setHandleExtension( );
        return (bool) array_search( $ext, $this->handleExtension);
    }

//    protected  function setHandleExtension( ){
//        if ( !$this->handleExtension ) {
//            foreach (get_declared_classes() as $class) {
//                if (is_subclass_of( $class, ArchiveReader::class ))
//
//                    $this->handleExtension[] = $class::getExtension();
//
//            }
//        }
//    }

    public  function getHandleExtension( ){

       // $this->setHandleExtension( );
       return $this->handleExtension;

    }


}