add file system class for missing file/dir creation with writability exception
This commit is contained in:
45
src/Utils/FileSystem/FileSystem.php
Normal file
45
src/Utils/FileSystem/FileSystem.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
namespace Bsr\Utils\FileSystem;
|
||||||
|
|
||||||
|
class FileSystem
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $path full path
|
||||||
|
* @throws Exception if not writable
|
||||||
|
*/
|
||||||
|
public static function createFileOrDirIfNotExists($path, $file=false, $throw=true)
|
||||||
|
{
|
||||||
|
if (file_exists($path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$notExistsPart = false;
|
||||||
|
$parts = explode('/', $path);
|
||||||
|
$partsCount = count($parts);
|
||||||
|
$buildPath = '';
|
||||||
|
for ($i=0; $i<$partsCount; $i++) {
|
||||||
|
$part = $parts[$i];
|
||||||
|
if (!file_exists($buildPath . "/$part")) {
|
||||||
|
if (!is_writable($builPath)) {
|
||||||
|
if ($throw) {
|
||||||
|
throw new Exception("Attempting to create : $path, but $buildPath is not writeable.");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$notExistsPart = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$buildPath .= "/$part";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($file) {
|
||||||
|
$fileName = array_pop($parts);
|
||||||
|
}
|
||||||
|
$dirPath = implode('/', $parts);
|
||||||
|
mkdir($dirPath, 0777, true);
|
||||||
|
if ($file) {
|
||||||
|
touch("$dirPath/$fileName");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
namespace Bsr\Utils\Logger;
|
namespace Bsr\Utils\Logger;
|
||||||
|
|
||||||
use Bsr\Utils\Configuration\Configuration;
|
use Bsr\Utils\Configuration\Configuration;
|
||||||
|
use Bsr\Utils\FileSystem\FileSystem;
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
|
|
||||||
@@ -158,12 +159,7 @@ class Logger {
|
|||||||
{
|
{
|
||||||
$mostRecentLogFileName = Configuration::get('log.file');
|
$mostRecentLogFileName = Configuration::get('log.file');
|
||||||
|
|
||||||
if (!file_exists($mostRecentLogFileName)) {
|
FileSystem::createFileOrDirIfNotExists($mostRecentLogFileName, true);
|
||||||
if (!is_dir(dirname($mostRecentLogFileName))) {
|
|
||||||
mkdir(dirname($mostRecentLogFileName), 0777, true);
|
|
||||||
}
|
|
||||||
touch($mostRecentLogFileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(self::isMostRecentLogFileFromYesterday()) {
|
if(self::isMostRecentLogFileFromYesterday()) {
|
||||||
self::makeRoomForNewLogFile($mostRecentLogFileName);
|
self::makeRoomForNewLogFile($mostRecentLogFileName);
|
||||||
|
|||||||
Reference in New Issue
Block a user