Skip to content

Commit

Permalink
Merge pull request #209 from koriym/validate-tmp-dir
Browse files Browse the repository at this point in the history
Validate tmp directory
  • Loading branch information
koriym committed Jan 19, 2024
2 parents 41fcf7d + 93ea388 commit 80b12a6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -44,8 +44,8 @@
"scripts" :{
"test": ["phpunit"],
"tests": ["@cs", "@test", "@sa"],
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage phpunit --coverage-text --coverage-html=build/coverage"],
"pcov": ["php -dextension=pcov.so -d pcov.enabled=1 phpunit --coverage-text --coverage-html=build/coverage --coverage-clover=coverage.xml"],
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"pcov": ["php -dextension=pcov.so -d pcov.enabled=1 ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage --coverage-clover=coverage.xml"],
"cs": ["phpcs --standard=./phpcs.xml sl-src src tests"],
"cs-fix": ["phpcbf sl-src src tests"],
"clean": ["phpstan clear-result-cache", "psalm --clear-cache", "rm -rf tests/tmp/*.php"],
Expand Down
55 changes: 31 additions & 24 deletions sl-src/Cache.php
@@ -1,26 +1,27 @@
<?php

declare(strict_types=1);
declare(strict_types=1);

namespace Ray\ServiceLocator;
namespace Ray\ServiceLocator;

use Ray\ServiceLocator\Exception\DirectoryNotWritableException;
use Ray\ServiceLocator\Exception\DirectoryNotWritableException;

use function file_exists;
use function file_put_contents;
use function hash;
use function is_writable;
use function mkdir;
use function pathinfo;
use function rename;
use function serialize;
use function substr;
use function tempnam;
use function unlink;
use function unserialize;
use function file_exists;
use function file_put_contents;
use function hash;
use function is_dir;
use function is_writable;
use function mkdir;
use function pathinfo;
use function rename;
use function serialize;
use function substr;
use function tempnam;
use function unlink;
use function unserialize;

use const DIRECTORY_SEPARATOR;
use const PATHINFO_DIRNAME;
use const DIRECTORY_SEPARATOR;
use const PATHINFO_DIRNAME;

/**
* Minimal cache
Expand All @@ -32,6 +33,10 @@ final class Cache

public function __construct(string $tmpDir)
{
if (! is_writable($tmpDir)) {
throw new DirectoryNotWritableException($tmpDir);
}

$this->tmpDir = $tmpDir;
}

Expand Down Expand Up @@ -61,16 +66,18 @@ private function getFilename(string $id): string
$hash = hash('crc32', $id);

$dir = $this->tmpDir
. DIRECTORY_SEPARATOR
. substr($hash, 0, 2);
if (! is_writable($dir)) {
mkdir($dir, 0777, true);
. DIRECTORY_SEPARATOR
. substr($hash, 0, 2);
if (! is_dir($dir) && ! @mkdir($dir) && ! is_dir($dir) && ! is_writable($dir)) {
// @codeCoverageIgnoreStart
throw new DirectoryNotWritableException($dir);
// @codeCoverageIgnoreEnd
}

return $dir
. DIRECTORY_SEPARATOR
. $hash
. '.php';
. DIRECTORY_SEPARATOR
. $hash
. '.php';
}

private function writeFile(string $filename, string $value): void
Expand Down

0 comments on commit 80b12a6

Please sign in to comment.