ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Handler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Export\ImportHandler\I\File\HandlerInterface as FileHandlerInterface;
24 use ILIAS\Export\ImportHandler\I\File\Namespace\CollectionInterface as FileNamespaceCollectionInterface;
25 use ILIAS\Export\ImportHandler\I\File\Namespace\FactoryInterface as FileNamespaceFactoryInterface;
26 use ILIAS\Export\ImportHandler\I\File\Namespace\HandlerInterface as FileNamespaceHandlerInterface;
27 use SplFileInfo;
28 
29 class Handler implements FileHandlerInterface
30 {
32  protected FileNamespaceFactoryInterface $namespace;
33  protected FileNamespaceCollectionInterface $namespaces;
34 
35  public function __construct(
36  FileNamespaceFactoryInterface $namespace
37  ) {
38  $this->namespace = $namespace;
39  $this->namespaces = $namespace->collection();
40  }
41 
42  public function withAdditionalNamespace(FileNamespaceHandlerInterface $namespace_handler): FileHandlerInterface
43  {
44  $clone = clone $this;
45  $clone->namespaces = $clone->namespaces->withElement($namespace_handler);
46  return $clone;
47  }
48 
49  public function getNamespaces(): FileNamespaceCollectionInterface
50  {
51  return $this->namespaces;
52  }
53 
54  public function withFileInfo(SplFileInfo $file_info): FileHandlerInterface
55  {
56  $clone = clone $this;
57  $clone->spl_file_info = $file_info;
58  return $clone;
59  }
60 
61  public function getFileName(): string
62  {
63  return $this->spl_file_info->getFilename();
64  }
65 
66  public function getFilePath(): string
67  {
68  return $this->fileExists()
69  ? $this->spl_file_info->getRealPath()
70  : $this->spl_file_info->getPath() . DIRECTORY_SEPARATOR . $this->spl_file_info->getFilename();
71  }
72 
73  public function getSubPathToDirBeginningAtPathEnd(string $dir_name): FileHandlerInterface
74  {
75  $parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
76  $trimmed_str = '';
77  for ($i = count($parts) - 1; $i >= 0; $i--) {
78  $trimmed_str = $i < count($parts) - 1
79  ? $parts[$i] . DIRECTORY_SEPARATOR . $trimmed_str
80  : $parts[$i];
81  if ($parts[$i] === $dir_name) {
82  break;
83  }
84  }
85  $clone = clone $this;
86  $clone->spl_file_info = new SplFileInfo($trimmed_str);
87  return $clone;
88  }
89 
90  public function getSubPathToDirBeginningAtPathStart(string $dir_name): FileHandlerInterface
91  {
92  $parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
93  $trimmed_str = '';
94  for ($i = 0; $i < count($parts); $i++) {
95  $trimmed_str .= $i > 0
96  ? DIRECTORY_SEPARATOR . $parts[$i]
97  : $parts[$i];
98  if ($parts[$i] === $dir_name) {
99  break;
100  }
101  }
102  $clone = clone $this;
103  $clone->spl_file_info = new SplFileInfo($trimmed_str);
104  return $clone;
105  }
106 
107  public function getPathToFileLocation(): string
108  {
109  return $this->spl_file_info->getPath();
110  }
111 
112  public function fileExists(): bool
113  {
114  return $this->spl_file_info->getRealPath() !== false;
115  }
116 
117  public function getPathPart(string $pattern): string|null
118  {
119  $path_parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
120  foreach ($path_parts as $path_part) {
121  if (preg_match($pattern, $path_part) === 1) {
122  return $path_part;
123  }
124  }
125  return null;
126  }
127 
128  public function pathContainsFolderName(string $folder_name): bool
129  {
130  $path_parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
131  if (in_array($folder_name, $path_parts, true)) {
132  return true;
133  }
134  return false;
135  }
136 }
FileNamespaceCollectionInterface $namespaces
Definition: Handler.php:33
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
__construct(FileNamespaceFactoryInterface $namespace)
Definition: Handler.php:35
withFileInfo(SplFileInfo $file_info)
Definition: Handler.php:54
getSubPathToDirBeginningAtPathEnd(string $dir_name)
Definition: Handler.php:73
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
pathContainsFolderName(string $folder_name)
Definition: Handler.php:128
FileNamespaceFactoryInterface $namespace
Definition: Handler.php:32
withAdditionalNamespace(FileNamespaceHandlerInterface $namespace_handler)
Definition: Handler.php:42
getSubPathToDirBeginningAtPathStart(string $dir_name)
Definition: Handler.php:90