edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem; |
||
| 4 | |||
| 5 | class File extends AbstractFilesystemItem |
||
| 6 | { |
||
| 7 | protected const PATH_TYPE = 'file'; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * The permissions to be set on newly created files |
||
| 11 | * |
||
| 12 | * @var int |
||
| 13 | */ |
||
| 14 | protected $createMode = 0644; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string|null |
||
| 18 | */ |
||
| 19 | protected $contents; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Stores an instance fo the SplFileObject object for the file. |
||
| 23 | * |
||
| 24 | * @var \SplFileObject |
||
| 25 | */ |
||
| 26 | protected $splFileObject; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Directory |
||
| 30 | */ |
||
| 31 | private $directory; |
||
| 32 | |||
| 33 | public function removeIfExists(): self |
||
| 34 | { |
||
| 35 | if ($this->exists()) { |
||
| 36 | unlink($this->path); |
||
| 37 | } |
||
| 38 | |||
| 39 | return $this; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $path |
||
| 44 | * |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | public function setPath(string $path): self |
||
| 48 | { |
||
| 49 | parent::setPath($path); |
||
| 50 | $this->directory = new Directory(\dirname($path)); |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getContents(): ?string |
||
| 59 | { |
||
| 60 | return $this->contents; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $contents |
||
| 65 | * |
||
| 66 | * @return File |
||
| 67 | */ |
||
| 68 | public function setContents(string $contents): File |
||
| 69 | { |
||
| 70 | $this->contents = $contents; |
||
| 71 | |||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return File |
||
| 77 | * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
||
| 78 | */ |
||
| 79 | public function putContents(): self |
||
| 80 | { |
||
| 81 | $this->assertExists(); |
||
| 82 | \ts\file_put_contents($this->path, $this->contents); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 83 | |||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return File |
||
| 89 | * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
||
| 90 | */ |
||
| 91 | public function loadContents(): self |
||
| 92 | { |
||
| 93 | $this->assertExists(); |
||
| 94 | $this->contents = \ts\file_get_contents($this->path); |
||
| 95 | |||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Provide an SplFileObject object, asserting that the path exists |
||
| 101 | * |
||
| 102 | * @return \SplFileObject |
||
| 103 | * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
||
| 104 | */ |
||
| 105 | public function getSplFileObject(): \SplFileObject |
||
| 106 | { |
||
| 107 | $this->assertExists(); |
||
| 108 | if (null !== $this->splFileObject && $this->path === $this->splFileObject->getRealPath()) { |
||
| 109 | return $this->splFileObject; |
||
| 110 | } |
||
| 111 | $this->splFileObject = new \SplFileObject($this->path); |
||
| 112 | |||
| 113 | return $this->splFileObject; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return Directory |
||
| 118 | */ |
||
| 119 | public function getDirectory(): Directory |
||
| 120 | { |
||
| 121 | return $this->directory; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * This is the specific creation logic for the concrete filesystem type. |
||
| 126 | */ |
||
| 127 | protected function doCreate(): void |
||
| 128 | { |
||
| 129 | $this->directory->assertExists(); |
||
| 130 | \ts\file_put_contents($this->path, ''); |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function isCorrectType(): bool |
||
| 134 | { |
||
| 135 | return $this->createSplFileInfo()->isFile(); |
||
| 136 | } |
||
| 137 | } |
||
| 138 |