Blame view

common/components/archive_reader/ArchiveCreator.php 1.39 KB
57e5a4b9   Mihail   add classes to wo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  <?php
  /**
   * Created by PhpStorm.
   * User: Tsurkanov
   * Date: 03.11.2015
   * Time: 14:51
   */
  
  namespace common\components\archive_reader;
  
  
  class ArchiveCreator {
  
    protected  $handleExtension = [];
  
      public  function create( $file, $ext ){
          if ( isHandableExtension( $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;
                  }
  
              }
  
          }
          // не найден подходящий обработчик
          throw new \Exception( "Для расширения {$ext} не найден подходящий распаковщик" );
      }
  
      protected  function isHandableExtension( $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( ){
  
         return $this->handleExtension;
  
      }
  
  
  }