Skip to content

Commit 995d04e

Browse files
committed
add support for file timestamps, fix creation of elements through links
1 parent 2e8b5d3 commit 995d04e

28 files changed

+864
-26
lines changed

src/Exception/TouchException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Aternos\IO\Exception;
4+
5+
/**
6+
* Class TouchException
7+
*
8+
* Thrown when a touch operation fails
9+
*
10+
* @package Aternos\IO\Exception
11+
*/
12+
class TouchException extends IOException
13+
{
14+
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface GetAccessTimestampInterface
10+
*
11+
* Allows getting the access timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface GetAccessTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Get the access timestamp of the element
19+
*
20+
* @throws IOException
21+
* @return int
22+
*/
23+
public function getAccessTimestamp(): int;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface GetBirthTimestampInterface
10+
*
11+
* Allows getting the birth timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface GetBirthTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Get the birth timestamp of the element
19+
*
20+
* @throws IOException
21+
* @return int
22+
*/
23+
public function getBirthTimestamp(): int;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface GetModificationTimestampInterface
10+
*
11+
* Allows getting the modification timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface GetModificationTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Get the modification timestamp of the element
19+
*
20+
* @throws IOException
21+
* @return int
22+
*/
23+
public function getModificationTimestamp(): int;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface GetStatusChangeTimestampInterface
10+
*
11+
* Allows getting the timestamp of the last status change
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface GetStatusChangeTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Get the timestamp of the last status change
19+
*
20+
* @throws IOException
21+
* @return int
22+
*/
23+
public function getStatusChangeTimestamp(): int;
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface SetAccessTimestampInterface
10+
*
11+
* Allows setting the access timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface SetAccessTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Set the access timestamp
19+
*
20+
* This might also change other timestamps
21+
*
22+
* @throws IOException
23+
* @param int $timestamp
24+
* @return $this
25+
*/
26+
public function setAccessTimestamp(int $timestamp): static;
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface SetBirthTimestampInterface
10+
*
11+
* Allows setting the birth timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface SetBirthTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Set the birth timestamp
19+
*
20+
* This might also change other timestamps
21+
*
22+
* @throws IOException
23+
* @param int $timestamp
24+
* @return $this
25+
*/
26+
public function setBirthTimestamp(int $timestamp): static;
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface SetModificationTimestampInterface
10+
*
11+
* Allows setting the modification timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface SetModificationTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Set the modification timestamp
19+
*
20+
* This might also change other timestamps
21+
*
22+
* @throws IOException
23+
* @param int $timestamp
24+
* @return $this
25+
*/
26+
public function setModificationTimestamp(int $timestamp): static;
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Interfaces\IOElementInterface;
7+
8+
/**
9+
* Interface SetStatusChangeTimestampInterface
10+
*
11+
* Allows setting the status change timestamp of an element
12+
*
13+
* @package Aternos\IO\Interfaces\Features
14+
*/
15+
interface SetStatusChangeTimestampInterface extends IOElementInterface
16+
{
17+
/**
18+
* Set the status change timestamp
19+
*
20+
* This might also change other timestamps
21+
*
22+
* @throws IOException
23+
* @param int $timestamp
24+
* @return $this
25+
*/
26+
public function setStatusChangeTimestamp(int $timestamp): static;
27+
}

src/System/FilesystemElement.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Aternos\IO\Exception\MoveException;
66
use Aternos\IO\Exception\PathOutsideElementException;
7+
use Aternos\IO\Exception\StatException;
8+
use Aternos\IO\Exception\TouchException;
79
use Aternos\IO\Interfaces\Features\GetPathInterface;
810
use Aternos\IO\System\Directory\Directory;
911
use Aternos\IO\System\File\File;
@@ -134,6 +136,84 @@ public function exists(): bool
134136
return file_exists($this->path);
135137
}
136138

139+
/**
140+
* @inheritDoc
141+
* @throws StatException
142+
*/
143+
public function getAccessTimestamp(): int
144+
{
145+
$time = @fileatime($this->path);
146+
if ($time === false) {
147+
$error = error_get_last();
148+
throw new StatException("Could not get access timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
149+
}
150+
return $time;
151+
}
152+
153+
/**
154+
* @inheritDoc
155+
* @throws StatException
156+
*/
157+
public function getModificationTimestamp(): int
158+
{
159+
$time = @filemtime($this->path);
160+
if ($time === false) {
161+
$error = error_get_last();
162+
throw new StatException("Could not get modification timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
163+
}
164+
return $time;
165+
}
166+
167+
/**
168+
* @inheritDoc
169+
* @throws StatException
170+
*/
171+
public function getStatusChangeTimestamp(): int
172+
{
173+
$time = @filectime($this->path);
174+
if ($time === false) {
175+
$error = error_get_last();
176+
throw new StatException("Could not get status change timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
177+
}
178+
return $time;
179+
}
180+
181+
/**
182+
* @inheritDoc
183+
* @throws TouchException
184+
* @throws StatException
185+
*/
186+
public function setAccessTimestamp(int $timestamp): static
187+
{
188+
if (!file_exists($this->path)) {
189+
throw new StatException("Could not set access timestamp because element does not exist (" . $this->path . ")", $this);
190+
}
191+
192+
if (!@touch($this->path, $timestamp, $timestamp)) {
193+
$error = error_get_last();
194+
throw new TouchException("Could not set access timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
195+
}
196+
return $this;
197+
}
198+
199+
/**
200+
* @inheritDoc
201+
* @throws TouchException
202+
* @throws StatException
203+
*/
204+
public function setModificationTimestamp(int $timestamp): static
205+
{
206+
if (!file_exists($this->path)) {
207+
throw new StatException("Could not set modification timestamp because element does not exist (" . $this->path . ")", $this);
208+
}
209+
210+
if (!@touch($this->path, mtime: $timestamp)) {
211+
$error = error_get_last();
212+
throw new TouchException("Could not set modification timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
213+
}
214+
return $this;
215+
}
216+
137217
/**
138218
* @return string[]
139219
*/

0 commit comments

Comments
 (0)