Skip to content

Commit

Permalink
add support for file timestamps, fix creation of elements through links
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Nov 15, 2024
1 parent 2e8b5d3 commit 995d04e
Show file tree
Hide file tree
Showing 28 changed files with 864 additions and 26 deletions.
15 changes: 15 additions & 0 deletions src/Exception/TouchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Aternos\IO\Exception;

/**
* Class TouchException
*
* Thrown when a touch operation fails
*
* @package Aternos\IO\Exception
*/
class TouchException extends IOException
{

}
24 changes: 24 additions & 0 deletions src/Interfaces/Features/GetAccessTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface GetAccessTimestampInterface
*
* Allows getting the access timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface GetAccessTimestampInterface extends IOElementInterface
{
/**
* Get the access timestamp of the element
*
* @throws IOException
* @return int
*/
public function getAccessTimestamp(): int;
}
24 changes: 24 additions & 0 deletions src/Interfaces/Features/GetBirthTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface GetBirthTimestampInterface
*
* Allows getting the birth timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface GetBirthTimestampInterface extends IOElementInterface
{
/**
* Get the birth timestamp of the element
*
* @throws IOException
* @return int
*/
public function getBirthTimestamp(): int;
}
24 changes: 24 additions & 0 deletions src/Interfaces/Features/GetModificationTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface GetModificationTimestampInterface
*
* Allows getting the modification timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface GetModificationTimestampInterface extends IOElementInterface
{
/**
* Get the modification timestamp of the element
*
* @throws IOException
* @return int
*/
public function getModificationTimestamp(): int;
}
24 changes: 24 additions & 0 deletions src/Interfaces/Features/GetStatusChangeTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface GetStatusChangeTimestampInterface
*
* Allows getting the timestamp of the last status change
*
* @package Aternos\IO\Interfaces\Features
*/
interface GetStatusChangeTimestampInterface extends IOElementInterface
{
/**
* Get the timestamp of the last status change
*
* @throws IOException
* @return int
*/
public function getStatusChangeTimestamp(): int;
}
27 changes: 27 additions & 0 deletions src/Interfaces/Features/SetAccessTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface SetAccessTimestampInterface
*
* Allows setting the access timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface SetAccessTimestampInterface extends IOElementInterface
{
/**
* Set the access timestamp
*
* This might also change other timestamps
*
* @throws IOException
* @param int $timestamp
* @return $this
*/
public function setAccessTimestamp(int $timestamp): static;
}
27 changes: 27 additions & 0 deletions src/Interfaces/Features/SetBirthTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface SetBirthTimestampInterface
*
* Allows setting the birth timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface SetBirthTimestampInterface extends IOElementInterface
{
/**
* Set the birth timestamp
*
* This might also change other timestamps
*
* @throws IOException
* @param int $timestamp
* @return $this
*/
public function setBirthTimestamp(int $timestamp): static;
}
27 changes: 27 additions & 0 deletions src/Interfaces/Features/SetModificationTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface SetModificationTimestampInterface
*
* Allows setting the modification timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface SetModificationTimestampInterface extends IOElementInterface
{
/**
* Set the modification timestamp
*
* This might also change other timestamps
*
* @throws IOException
* @param int $timestamp
* @return $this
*/
public function setModificationTimestamp(int $timestamp): static;
}
27 changes: 27 additions & 0 deletions src/Interfaces/Features/SetStatusChangeTimestampInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Interfaces\IOElementInterface;

/**
* Interface SetStatusChangeTimestampInterface
*
* Allows setting the status change timestamp of an element
*
* @package Aternos\IO\Interfaces\Features
*/
interface SetStatusChangeTimestampInterface extends IOElementInterface
{
/**
* Set the status change timestamp
*
* This might also change other timestamps
*
* @throws IOException
* @param int $timestamp
* @return $this
*/
public function setStatusChangeTimestamp(int $timestamp): static;
}
80 changes: 80 additions & 0 deletions src/System/FilesystemElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Aternos\IO\Exception\MoveException;
use Aternos\IO\Exception\PathOutsideElementException;
use Aternos\IO\Exception\StatException;
use Aternos\IO\Exception\TouchException;
use Aternos\IO\Interfaces\Features\GetPathInterface;
use Aternos\IO\System\Directory\Directory;
use Aternos\IO\System\File\File;
Expand Down Expand Up @@ -134,6 +136,84 @@ public function exists(): bool
return file_exists($this->path);
}

/**
* @inheritDoc
* @throws StatException
*/
public function getAccessTimestamp(): int
{
$time = @fileatime($this->path);
if ($time === false) {
$error = error_get_last();
throw new StatException("Could not get access timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $time;
}

/**
* @inheritDoc
* @throws StatException
*/
public function getModificationTimestamp(): int
{
$time = @filemtime($this->path);
if ($time === false) {
$error = error_get_last();
throw new StatException("Could not get modification timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $time;
}

/**
* @inheritDoc
* @throws StatException
*/
public function getStatusChangeTimestamp(): int
{
$time = @filectime($this->path);
if ($time === false) {
$error = error_get_last();
throw new StatException("Could not get status change timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $time;
}

/**
* @inheritDoc
* @throws TouchException
* @throws StatException
*/
public function setAccessTimestamp(int $timestamp): static
{
if (!file_exists($this->path)) {
throw new StatException("Could not set access timestamp because element does not exist (" . $this->path . ")", $this);
}

if (!@touch($this->path, $timestamp, $timestamp)) {
$error = error_get_last();
throw new TouchException("Could not set access timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $this;
}

/**
* @inheritDoc
* @throws TouchException
* @throws StatException
*/
public function setModificationTimestamp(int $timestamp): static
{
if (!file_exists($this->path)) {
throw new StatException("Could not set modification timestamp because element does not exist (" . $this->path . ")", $this);
}

if (!@touch($this->path, mtime: $timestamp)) {
$error = error_get_last();
throw new TouchException("Could not set modification timestamp (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $this;
}

/**
* @return string[]
*/
Expand Down
15 changes: 14 additions & 1 deletion src/System/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use Aternos\IO\Interfaces\Features\DeleteInterface;
use Aternos\IO\Interfaces\Features\ExistsInterface;
use Aternos\IO\Interfaces\Features\GetAccessTimestampInterface;
use Aternos\IO\Interfaces\Features\GetModificationTimestampInterface;
use Aternos\IO\Interfaces\Features\GetStatusChangeTimestampInterface;
use Aternos\IO\Interfaces\Features\MovePathInterface;
use Aternos\IO\Interfaces\Features\SetAccessTimestampInterface;
use Aternos\IO\Interfaces\Features\SetModificationTimestampInterface;

/**
* Interface FilesystemInterface
Expand All @@ -13,7 +18,15 @@
*
* @package Aternos\IO\System
*/
interface FilesystemInterface extends MovePathInterface, DeleteInterface, ExistsInterface
interface FilesystemInterface extends
MovePathInterface,
DeleteInterface,
ExistsInterface,
GetAccessTimestampInterface,
GetModificationTimestampInterface,
GetStatusChangeTimestampInterface,
SetAccessTimestampInterface,
SetModificationTimestampInterface
{

}
15 changes: 13 additions & 2 deletions src/System/Link/DirectoryLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Aternos\IO\Interfaces\Features\GetPathInterface;
use Aternos\IO\Interfaces\Types\DirectoryInterface;
use Aternos\IO\Interfaces\Types\Link\DirectoryLinkInterface;
use Aternos\IO\System\Directory\Directory;
use Generator;

/**
Expand All @@ -25,11 +26,21 @@ class DirectoryLink extends Link implements DirectoryLinkInterface
*/
public function getTarget(): DirectoryInterface
{
$target = parent::getTarget();
if ($this->target) {
return $this->target;
}

$targetPath = $this->getTargetPath();

if (!$this->targetExists()) {
return $this->target = new Directory($targetPath);
}

$target = static::getIOElementFromPath($targetPath);
if (!$target instanceof DirectoryInterface) {
throw new GetTargetException("Could not get directory link target because link target is not a directory (" . $this->path . ")", $this);
}
return $target;
return $this->target = $target;
}

/**
Expand Down
Loading

0 comments on commit 995d04e

Please sign in to comment.