Attention needed

Saeghe is a difficult name to pronounce. Therefore, Saeghe project has been renamed to phpkg.

This website no longer receives updates.

Please visit phpkg website at phpkg.com

Introduction

The Symlink class extends Datatype Text abstract class. You can use it to keep any file path as a string and make sure the path has been validated. It also gives you access to an API that you can see in the following.

Note For more information on the Text class, please read its documentation

Usage

You can make a new Symlink instance and use it like so:

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = Symlink::from_string('/root/home/user/symlink');
echo $symlink; // Output: '/root/home/user/symlink'

$symlink = new Path('/root/home/user/symlink');
echo $symlink; // Output: '/root/home/user/symlink'

Or you can use a Path instance:

use Saeghe\FileManager\Filesystem\Symlink;
use Saeghe\FileManager\Path;

$path = Path::from_string('/root/home/user/symlink');
$symlink = new Symlink($path);
echo $symlink; // Output: '/root/home/user/symlink'

Note For more information on the Path class, please read its documentation

The Symlink class resolves the given string by using the Resolver\realpath function.

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = Symlink::from_string('/root/home/user/../project/symlink');
echo $symlink; // Output: '/root/home/project/symlink'

Note For more information on the Resolver functions, please read its documentation

Methods

Here you can see a list of the available methods on the Symlink class:

exists

You can use the exists method to check if the given symlink exists on the filesystem.

public function exists(): bool

Example

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = Symlink::from_string('/home/user/symlink');

echo (int) $symlink->exists(); // Output: 1
echo (int) $symlink->delete()->exists(); // Output: 0

leaf

You can use the leaf method to get the leaf of the symlink which is the filename of the symlink.

public function leaf(): string

Example

use Saeghe\FileManager\Filesystem\Symlink;

echo Path::from_string('/home/user/project')->leaf(); // Output: 'project'
echo Path::from_string('/home/user/project/filename.txt')->leaf(); // Output: 'filename.txt' 

parent

The parent method returns an instance of the Directory class from the current path's parent directory.

Note For more information on the Directory class, please read its documentation

public function parent(): Directory

Example

use Saeghe\FileManager\Filesystem\Symlink;

echo Path::from_string('/home/user/symlink')->parent(); // Output: '/home/user'
echo Path::from_string('/home/user/project/symlink')->parent(); // Output: '/home/user/project' 

relocate

The relocate method returns a new Path instance by replacing the given origin with the given destination.

public function relocate(string $origin, string $destination): Path

Example

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = new Path('/home/user/directory/symlink');
$relocate = $symlink->relocate('/home/user/directory', '/home/user2/directory/../another-directory');
echo $relocate; // Output: '/home/user2/another-directory/symlink' 

sibling

The sibling method returns a new Path of the given path base on the path parent directory.

public function sibling(string $symlink): Path

Example

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = new Path('/home/user/directory/symlink');
echo $sibling_directory = $symlink->sibling('subdirectory'); // Output: /home/user/directory/subdirectory
echo $sibling_filename = $symlink->sibling('other-file.extension'); // Output: /home/user/directory/other-file.extension

delete

The delete deletes the symlink from the filesystem.

public function delete(): self

Example

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = new Path('/home/user/directory/symlink');
echo (int) $symlink->exists(); // Output: 1
$symlink->delete();
echo (int) $symlink->exists(); // Output: 0

exists

The exists returns true if the symlink exists on the filesystem and is a link. Otherwise, it returns false.

public function exists(): bool

Example

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = new Path('/home/user/directory/symlink');
echo (int) $symlink->exists(); // Output: 0
$symlink->link(File::from_string('/home/user/file'));
echo (int) $symlink->exists(); // Output: 1

link

The link method creates the symlink on the filesystem and links it to the given file.

public function link(File $file): self

Example

use Saeghe\FileManager\Filesystem\Symlink;

$symlink = new Path('/home/user/directory/symlink');
echo (int) $symlink->exists(); // Output: 0
$symlink->link(File::from_string('/home/user/file'));
echo (int) $symlink->exists(); // Output: 1