1<?php
2
3/**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7
8declare(strict_types=1);
9
10namespace Nette\Utils;
11
12use Nette;
13
14
15/**
16 * Represents the file or directory returned by the Finder.
17 * @internal do not create instances directly
18 */
19final class FileInfo extends \SplFileInfo
20{
21	private string $relativePath;
22
23
24	public function __construct(string $file, string $relativePath = '')
25	{
26		parent::__construct($file);
27		$this->setInfoClass(static::class);
28		$this->relativePath = $relativePath;
29	}
30
31
32	/**
33	 * Returns the relative directory path.
34	 */
35	public function getRelativePath(): string
36	{
37		return $this->relativePath;
38	}
39
40
41	/**
42	 * Returns the relative path including file name.
43	 */
44	public function getRelativePathname(): string
45	{
46		return ($this->relativePath === '' ? '' : $this->relativePath . DIRECTORY_SEPARATOR)
47			. $this->getBasename();
48	}
49
50
51	/**
52	 * Returns the contents of the file.
53	 * @throws Nette\IOException
54	 */
55	public function read(): string
56	{
57		return FileSystem::read($this->getPathname());
58	}
59
60
61	/**
62	 * Writes the contents to the file.
63	 * @throws Nette\IOException
64	 */
65	public function write(string $content): void
66	{
67		FileSystem::write($this->getPathname(), $content);
68	}
69}
70