/src/File/BinaryFile.php
https://bitbucket.org/wirbelwild/kiwa-core · PHP · 215 lines · 109 code · 36 blank · 70 comment · 11 complexity · be423364f465762844e7f8a4cc5d2978 MD5 · raw file
- <?php
- /**
- * Kiwa. A feather light web framework for small but professional static websites.
- *
- * @author Tobias Köngeter
- * @copyright Copyright © 2020 Bit&Black
- * @link https://www.bitandblack.com
- * @link https://www.kiwa.io
- * @license MIT
- */
- namespace Kiwa\File;
- use DateTime;
- use Exception;
- use Kiwa\DI;
- use Kiwa\Path;
- use Symfony\Component\Mime\MimeTypes;
- /**
- * The BinaryFile class holds information about a binary file.
- *
- * @package Kiwa\File
- */
- final class BinaryFile extends AbstractFile
- {
- /**
- * @var string
- */
- private $filePath;
-
- /**
- * @var int
- */
- private $status;
-
- /**
- * @var string|null
- */
- private $mimeType;
- /**
- * @var \DateTime|null
- */
- private $lastModified;
- /**
- * Creates a new binary file object.
- *
- * @param string $file Full path to the file.
- */
- public function __construct(string $file)
- {
- $this->status = 200;
-
- $extension = pathinfo($file, PATHINFO_EXTENSION);
- if ('' === $extension) {
- $this->status = 403;
- return;
- }
- if (null === $fileNameReal = $this->getFileNameIfExists($file)) {
- DI::getLog()->debug('File doesn\'t exist.');
- $this->status = 404;
- return;
- }
- $this->filePath = $fileNameReal;
-
- $mimeTypes = MimeTypes::getDefault()->getMimeTypes($extension);
- $this->mimeType = $mimeTypes[0] ?? 'application/octet-stream';
- if (false !== $lastModified = DateTime::createFromFormat('U', (string) filemtime($file))) {
- $this->lastModified = $lastModified;
- }
- }
- /**
- * Returns a files name if it exists.
- * This method is case insensitive.
- *
- * @param string $fileName
- * @return null|string
- */
- private function getFileNameIfExists(string $fileName): ?string
- {
- if (file_exists($fileName)) {
- return $fileName;
- }
- $directoryName = dirname($fileName);
- $files = glob($directoryName.DIRECTORY_SEPARATOR.'*', GLOB_NOSORT);
- $fileNameLowerCase = mb_strtolower($fileName);
- if (!is_array($files)) {
- return null;
- }
- foreach ($files as $file) {
- if (mb_strtolower($file) === $fileNameLowerCase) {
- return $file;
- }
- }
- return null;
- }
-
- /**
- * Reads a file.
- *
- * @return string|null
- */
- public function readFile(): ?string
- {
- if (null === $file = $this->getFileNameIfExists($this->getFilePath())) {
- return null;
- }
- $output = (string) file_get_contents($file);
- /**
- * Declare kiwa variable to use in template files.
- *
- * @deprecated
- * @todo Remove in v1.0.
- */
- $kiwa = self::$controller;
- if (false !== strpos($file, '.php')) {
- try {
- ob_start();
- include $file;
- $output = (string) ob_get_clean();
- } catch (Exception $exception) {
- $output = (string) $exception;
- $output.= PHP_EOL.PHP_EOL;
- $output.= ob_get_clean();
- }
- }
- return $output;
- }
- /**
- * Gets the full path to the file.
- *
- * @return string
- */
- public function getFilePath(): string
- {
- return $this->filePath;
- }
- /**
- * Gets the http status code of the file.
- *
- * @return int
- */
- public function getStatus(): int
- {
- return $this->status;
- }
- /**
- * Gets the mime type of the file.
- *
- * @return string|null
- */
- public function getMimeType(): ?string
- {
- return $this->mimeType;
- }
- /**
- * @return bool
- */
- public function isVersionedFile(): bool
- {
- $manifestJson = Path::getBuildFolder().DIRECTORY_SEPARATOR.'manifest.json';
- if (!file_exists($manifestJson)) {
- return false;
- }
-
- $manifest = (string) file_get_contents($manifestJson);
- $manifest = json_decode($manifest, true);
-
- if (null === $manifest) {
- $manifest = [];
- }
-
- $file = pathinfo($this->getFilePath(), PATHINFO_BASENAME);
-
- foreach ($manifest as $shortName => $versionedName) {
- $shortName = pathinfo($shortName, PATHINFO_BASENAME);
- $versionedName = pathinfo($versionedName, PATHINFO_BASENAME);
-
- if ($versionedName === $file) {
- return $shortName !== $versionedName;
- }
- }
-
- return false;
- }
- /**
- * @return \DateTime|null
- */
- public function getLastModified(): ?DateTime
- {
- return $this->lastModified;
- }
- }