Blame view

common/components/archives/ArchiveCreator.php 2.97 KB
4f3f27e8   Mihail   temp commit - tes...
1
2
3
4
5
6
7
8
9
10
11
  <?php
  /**
   * Created by PhpStorm.
   * User: Tsurkanov
   * Date: 03.11.2015
   * Time: 14:51
   */
  
  namespace common\components\archives;
  
  
d95262f3   Mihail   finish with mail ...
12
13
14
15
16
  /**
   * Class ArchiveCreator
   * @package common\components\archives
   * фабрика по созданию объектов ArchiveReader
   */
4f3f27e8   Mihail   temp commit - tes...
17
18
  class ArchiveCreator {
  
d95262f3   Mihail   finish with mail ...
19
20
21
22
23
24
25
      //@todo - переделать на автоматическое определние поддерживаемых расширений и создание архиватора динамически из имени расширения
      // сейчас не работает, так как class_exists( $arh_class ) - неработает. Класс не лоадится почему-то
      // так-же смежная проблема - классов ридеров нет в get_declared_classes()
      /**
       * @var array - массив поддерживаемых расширений
       */
      protected  $handleExtension = ['rar', 'zip'];
4f3f27e8   Mihail   temp commit - tes...
26
  
d95262f3   Mihail   finish with mail ...
27
28
29
30
31
32
      /**
       * @param $file - имя файла для распаковки
       * @param $ext - расширение файла для распаковки
       * @return экземпляр ArchiveReader
       * @throws \Exception
       */
4f3f27e8   Mihail   temp commit - tes...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      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;
  //                }
  //
  //            }
  //
  //        }
d95262f3   Mihail   finish with mail ...
48
          if ( $ext  == 'zip' ) {
4f3f27e8   Mihail   temp commit - tes...
49
              $arh_reader =  new ZipArchiveReader();
d95262f3   Mihail   finish with mail ...
50
          }elseif( $ext  == 'rar' ){
4f3f27e8   Mihail   temp commit - tes...
51
52
53
              $arh_reader =  new RarArchiveReader();
          }
  
d95262f3   Mihail   finish with mail ...
54
55
56
57
  
          if( $arh_reader->open( $file ) )
              return $arh_reader;
  
4f3f27e8   Mihail   temp commit - tes...
58
59
60
61
          // не найден подходящий обработчик
          throw new \Exception( "Для расширения {$ext} не найден подходящий распаковщик" );
      }
  
d95262f3   Mihail   finish with mail ...
62
63
64
65
66
      /**
       * проверяет поддерживается ли полученное расширение нашими ридерами
       * @param $ext - расширение, которое нужно проверить
       * @return bool
       */
4f3f27e8   Mihail   temp commit - tes...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
      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;
  
      }
  
  
  }