‪TYPO3CMS  10.4
LocalPath.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use LogicException;
23 
29 {
30  public const ‪TYPE_ABSOLUTE = 1;
31  public const ‪TYPE_RELATIVE = 2;
32 
36  protected ‪$raw;
37 
41  protected ‪$relative;
42 
46  protected ‪$absolute;
47 
51  protected ‪$type;
52 
53  public function ‪__construct(string $value, int ‪$type)
54  {
55  if (‪$type !== self::TYPE_ABSOLUTE && ‪$type !== self::TYPE_RELATIVE) {
56  throw new LogicException(sprintf('Unexpected type "%d"', ‪$type), 1625826491);
57  }
58 
59  // @todo `../` is erased here, check again if this is a valid scenario
60  // value and absolute have leading and trailing slash, e.g. '/some/path/'
61  $value = '/' . trim(‪PathUtility::getCanonicalPath($value), '/');
62  $value .= $value !== '/' ? '/' : '';
63  $this->raw = $value;
64  $this->type = ‪$type;
65 
66  $publicPath = ‪Environment::getPublicPath();
67  if (‪$type === self::TYPE_RELATIVE) {
68  $this->relative = $value;
69  $this->absolute = ‪PathUtility::getCanonicalPath($publicPath . $value) . '/';
70  } elseif (‪$type === self::TYPE_ABSOLUTE) {
71  $this->absolute = $value;
72  $this->relative = strpos($value, $publicPath) === 0
73  ? substr($value, strlen($publicPath))
74  : null;
75  }
76  }
77 
81  public function ‪getRaw(): string
82  {
83  return ‪$this->raw;
84  }
85 
89  public function ‪getRelative(): ?string
90  {
91  return ‪$this->relative;
92  }
93 
97  public function ‪getAbsolute(): string
98  {
99  return ‪$this->absolute;
100  }
101 
102  public function ‪isAbsolute(): bool
103  {
104  return $this->type === ‪self::TYPE_ABSOLUTE;
105  }
106 
107  public function ‪isRelative(): bool
108  {
109  return $this->type === ‪self::TYPE_RELATIVE;
110  }
111 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_RELATIVE
‪const TYPE_RELATIVE
Definition: LocalPath.php:31
‪TYPO3\CMS\Core\Resource\LocalPath\__construct
‪__construct(string $value, int $type)
Definition: LocalPath.php:49
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath($path)
Definition: PathUtility.php:307
‪TYPO3\CMS\Core\Resource\LocalPath\getRelative
‪string null getRelative()
Definition: LocalPath.php:85
‪TYPO3\CMS\Core\Resource\LocalPath\isRelative
‪isRelative()
Definition: LocalPath.php:103
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_ABSOLUTE
‪const TYPE_ABSOLUTE
Definition: LocalPath.php:30
‪TYPO3\CMS\Core\Resource\LocalPath\getAbsolute
‪string getAbsolute()
Definition: LocalPath.php:93
‪TYPO3\CMS\Core\Resource\LocalPath\$raw
‪string $raw
Definition: LocalPath.php:35
‪TYPO3\CMS\Core\Resource\LocalPath\getRaw
‪string getRaw()
Definition: LocalPath.php:77
‪TYPO3\CMS\Core\Resource\LocalPath\$absolute
‪string $absolute
Definition: LocalPath.php:43
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\LocalPath\$relative
‪string null $relative
Definition: LocalPath.php:39
‪TYPO3\CMS\Core\Resource\LocalPath
Definition: LocalPath.php:29
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Resource\LocalPath\isAbsolute
‪isAbsolute()
Definition: LocalPath.php:98
‪TYPO3\CMS\Core\Resource\LocalPath\$type
‪int $type
Definition: LocalPath.php:47