params = $params; } /** * @param string $path * @param string $type [directory or file] * @param string $content [file content] * * @return string|IOExceptionInterface */ public function getAbsolutePath(string $path, string $type, string $content = "", bool $force = false) { $absolutePath = $this->params->get('arborescence_base'); if ($path) { $absolutePath .= DIRECTORY_SEPARATOR . $path; } if ($type === "directory") { $absolutePath .= DIRECTORY_SEPARATOR; } $filesystem = new Filesystem(); $existingPath = $this->findPath($absolutePath); if ($existingPath && !$force) { $result = $absolutePath; } else { $created = $this->createPath($filesystem, $absolutePath, $type, $content); if (!$created instanceof Exception) { $result = $absolutePath; } else { throw new UnprocessableEntityMaestroException($created); } } return $result; } /** * @param string $path * * @return bool */ public function findPath(string $path): bool { $filesystem = new Filesystem(); $exist = $filesystem->exists($path); return $exist; } /** * @param string $path * * @return bool|Exception */ public function removeFromPath(string $path) { try { $filesystem = new Filesystem(); $filesystem->remove($path); $result = true; } catch (Exception $e) { $result = $e; } return $result; } /** * @param Filesystem $filesystem * @param string $path * @param string $content * * @return bool|Exception|IOExceptionInterface */ private function createPath(Filesystem $filesystem, string $path, string $type, string $content) { try { $result = true; if ('directory' === $type) { $filesystem->mkdir($path); } elseif ('file' === $type) { $filesystem->appendToFile($path, $content); } else { $result = new Exception("Error on path type, only 'directory' or 'file' allowed, $type given."); } } catch (IOExceptionInterface $exception) { $result = $exception; } return $result; } /** * @param string $path * @param string $file * * @throws NotFoundMaestroException * * @return string */ public function getFilePath(string $path, string $file): string { $absolutePath = $this->getAbsolutePath($path, 'directory'); $finder = new Finder(); $finder->files($file)->in($absolutePath); if ($finder->hasResults()) { if ($path) { $filePath = DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file; // Formatted for Twig render } else { $filePath = DIRECTORY_SEPARATOR . $file; } return $filePath; } else { throw new NotFoundMaestroException("Directory '$absolutePath' and file '$file'."); } } }