-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from exodus4d/develop
v1.0.0
- Loading branch information
Showing
4 changed files
with
114 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: exodu | ||
* Date: 03.09.2017 | ||
* Time: 17:02 | ||
*/ | ||
|
||
namespace Exodus4D\Socket\Main\Handler; | ||
|
||
|
||
class LogFileHandler { | ||
|
||
const ERROR_DIR_CREATE = 'There is no existing directory at "%s" and its not buildable.'; | ||
|
||
/** | ||
* steam uri | ||
* @var string | ||
*/ | ||
private $stream = ''; | ||
|
||
/** | ||
* stream dir | ||
* @var string | ||
*/ | ||
private $dir = '.'; | ||
|
||
/** | ||
* file base dir already created | ||
* @var bool | ||
*/ | ||
private $dirCreated = false; | ||
|
||
public function __construct(string $stream){ | ||
$this->stream = $stream; | ||
$this->dir = dirname($this->stream); | ||
$this->createDir(); | ||
} | ||
|
||
/** | ||
* write log data into to file | ||
* @param array $log | ||
*/ | ||
public function write(array $log){ | ||
$log = (string)json_encode($log, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | ||
if( !empty($log) ){ | ||
$stream = fopen($this->stream, 'a'); | ||
flock($stream, LOCK_EX); | ||
fwrite($stream, $log . PHP_EOL); | ||
flock($stream, LOCK_UN); | ||
fclose($stream); | ||
} | ||
} | ||
|
||
/** | ||
* create directory | ||
*/ | ||
private function createDir(){ | ||
// Do not try to create dir if it has already been tried. | ||
if ($this->dirCreated){ | ||
return; | ||
} | ||
|
||
if ($this->dir && !is_dir($this->dir)){ | ||
$status = mkdir($this->dir, 0777, true); | ||
if (false === $status) { | ||
throw new \UnexpectedValueException(sprintf(self::ERROR_DIR_CREATE, $this->dir)); | ||
} | ||
} | ||
$this->dirCreated = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters