‪TYPO3CMS  ‪main
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 
22 
28 {
29  public const ‪TYPE_ABSOLUTE = 1;
30  public const ‪TYPE_RELATIVE = 2;
31 
32  protected string ‪$raw;
33  protected ?string ‪$relative = null;
34  protected string ‪$absolute;
35  protected int ‪$type;
36 
37  public function ‪__construct(string $value, int ‪$type)
38  {
39  if (‪$type !== self::TYPE_ABSOLUTE && ‪$type !== self::TYPE_RELATIVE) {
40  throw new \LogicException(sprintf('Unexpected type "%d"', ‪$type), 1625826491);
41  }
42 
43  // @todo `../` is erased here, check again if this is a valid scenario
44  // value and absolute have leading and trailing slash, e.g. '/some/path/'
45  $value = '/' . trim(‪PathUtility::getCanonicalPath($value), '/');
46  $value .= $value !== '/' ? '/' : '';
47  $this->raw = $value;
48  $this->type = ‪$type;
49 
50  $publicPath = ‪Environment::getPublicPath();
51  if (‪$type === self::TYPE_RELATIVE) {
52  $this->relative = $value;
53  $this->absolute = ‪PathUtility::getCanonicalPath($publicPath . $value) . '/';
54  } elseif (‪$type === self::TYPE_ABSOLUTE) {
55  $this->absolute = $value;
56  $this->relative = str_starts_with($value, $publicPath)
57  ? substr($value, strlen($publicPath))
58  : null;
59  }
60  }
61 
65  public function ‪getRaw(): string
66  {
67  return ‪$this->raw;
68  }
69 
73  public function ‪getRelative(): ?string
74  {
75  return ‪$this->relative;
76  }
77 
81  public function ‪getAbsolute(): string
82  {
83  return ‪$this->absolute;
84  }
85 
86  public function ‪isAbsolute(): bool
87  {
88  return $this->type === ‪self::TYPE_ABSOLUTE;
89  }
90 
91  public function ‪isRelative(): bool
92  {
93  return $this->type === ‪self::TYPE_RELATIVE;
94  }
95 }
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath(string $path)
Definition: PathUtility.php:364
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_RELATIVE
‪const TYPE_RELATIVE
Definition: LocalPath.php:30
‪TYPO3\CMS\Core\Resource\LocalPath\__construct
‪__construct(string $value, int $type)
Definition: LocalPath.php:37
‪TYPO3\CMS\Core\Resource\LocalPath\getRelative
‪string null getRelative()
Definition: LocalPath.php:73
‪TYPO3\CMS\Core\Resource\LocalPath\isRelative
‪isRelative()
Definition: LocalPath.php:91
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_ABSOLUTE
‪const TYPE_ABSOLUTE
Definition: LocalPath.php:29
‪TYPO3\CMS\Core\Resource\LocalPath\getAbsolute
‪string getAbsolute()
Definition: LocalPath.php:81
‪TYPO3\CMS\Core\Resource\LocalPath\$raw
‪string $raw
Definition: LocalPath.php:32
‪TYPO3\CMS\Core\Resource\LocalPath\getRaw
‪string getRaw()
Definition: LocalPath.php:65
‪TYPO3\CMS\Core\Resource\LocalPath\$absolute
‪string $absolute
Definition: LocalPath.php:34
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\LocalPath
Definition: LocalPath.php:28
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Resource\LocalPath\isAbsolute
‪isAbsolute()
Definition: LocalPath.php:86
‪TYPO3\CMS\Core\Resource\LocalPath\$relative
‪string $relative
Definition: LocalPath.php:33
‪TYPO3\CMS\Core\Resource\LocalPath\$type
‪int $type
Definition: LocalPath.php:35