Skip to content

Commit 01eba03

Browse files
pierreshmichaelw85Pierre Clavequin
authored
refactor: merge yosymfony/resource-watcher (#161)
* Bump symfony package version and update resource watcher * Force new version * Revert change * Update test run with php 8.3 and re-add older phpunit versions * Remove doc blocks in favor of annotations * refactor: merge yosymfony/resource-watcher Include yosymfony/resource-watcher directly in PHPUnit watcher so that it is easier to manage dependencies. yosymfony/resource-watcher#12 Minimum PHP version 8.1 Require Symfony 6 or 7 --------- Co-authored-by: Michael Withagen <michael@lyfter.nl> Co-authored-by: Michael Withagen <michael.withagen@live.com> Co-authored-by: Pierre Clavequin <pierre.clavequin@valueapex.com>
1 parent 9cbe85b commit 01eba03

24 files changed

+1247
-30
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.2, 8.1, 8.0]
12+
php: [8.3, 8.2, 8.1, 8.0]
1313
dependency-version: [prefer-lowest, prefer-stable]
1414

1515
name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ build
22
composer.lock
33
vendor
44
.phpunit.result.cache
5+
.phpunit.cache
56
.php-cs-fixer.cache

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.2 | ^8.0 | ^8.1",
19+
"php": "^8.1",
2020
"clue/stdio-react": "^2.4",
2121
"jolicode/jolinotif": "^2.2",
22-
"symfony/console": "^5 | ^6",
23-
"symfony/finder": "^5.4 | ^6",
24-
"symfony/process": "^5.4 | ^6",
25-
"symfony/yaml": "^5.2 | ^6",
26-
"yosymfony/resource-watcher": "^2.0 | ^3.0"
22+
"symfony/console": "^6 | ^7",
23+
"symfony/finder": "^6 | ^7",
24+
"symfony/process": "^6 | ^7",
25+
"symfony/yaml": "^6 | ^7"
2726
},
2827
"conflict": {
29-
"yosymfony/resource-watcher": "<2.0",
3028
"symfony/console": "<5.2"
3129
},
3230
"require-dev": {
33-
"phpunit/phpunit": "^8.6 | ^9.0"
31+
"symfony/filesystem": "^6 | ^7",
32+
"phpunit/phpunit": "^10.0 | ^11.0"
3433
},
3534
"autoload": {
3635
"psr-4": {

phpunit.xml.dist

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
8-
<testsuites>
9-
<testsuite name="Spatie Test Suite">
10-
<directory>tests</directory>
11-
</testsuite>
12-
</testsuites>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
4+
<testsuites>
5+
<testsuite name="Spatie Test Suite">
6+
<directory>tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<source>
10+
<include>
11+
<directory suffix=".php">src/</directory>
12+
</include>
13+
</source>
1314
</phpunit>

src/ConsoleApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct()
1313
$this->add(new WatcherCommand());
1414
}
1515

16-
public function getLongVersion()
16+
public function getLongVersion(): string
1717
{
1818
return parent::getLongVersion().' by <comment>Spatie</comment>';
1919
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Yo! Symfony Resource Watcher.
5+
*
6+
* (c) YoSymfony <http://github.com/yosymfony>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spatie\PhpUnitWatcher\ResourceWatcher;
13+
14+
/**
15+
* CRC32 content hash implementation.
16+
*
17+
* @author Victor Puertas <vpgugr@gmail.com>
18+
*/
19+
class Crc32ContentHash implements HashInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function hash($filepath)
25+
{
26+
$fileContent = $filepath;
27+
28+
if (!\is_dir($filepath)) {
29+
$fileContent = file_get_contents($filepath);
30+
}
31+
32+
return hash('crc32', $fileContent);
33+
}
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Yo! Symfony Resource Watcher.
5+
*
6+
* (c) YoSymfony <http://github.com/yosymfony>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spatie\PhpUnitWatcher\ResourceWatcher;
13+
14+
/**
15+
* CRC32 content hash implementation.
16+
*
17+
* @author Victor Puertas <vpgugr@gmail.com>
18+
*/
19+
class Crc32MetaDataHash implements HashInterface
20+
{
21+
/** @var bool */
22+
protected $clearStatCache;
23+
24+
/**
25+
* Assign option to clear the file stat() cache.
26+
* @param bool $clearStatCache
27+
*/
28+
public function __construct($clearStatCache = false)
29+
{
30+
$this->clearStatCache = $clearStatCache;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function hash($filepath)
37+
{
38+
if ($this->clearStatCache) {
39+
clearstatcache(true, $filepath);
40+
}
41+
42+
$data = stat($filepath);
43+
44+
$str = basename($filepath) . $data['size'] . $data['mtime'] . $data['mode'];
45+
46+
return hash('crc32', $str);
47+
}
48+
}

src/ResourceWatcher/HashInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Yo! Symfony Resource Watcher.
5+
*
6+
* (c) YoSymfony <http://github.com/yosymfony>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spatie\PhpUnitWatcher\ResourceWatcher;
13+
14+
/**
15+
* Interface for hashing a file.
16+
*
17+
* @author Victor Puertas <vpgugr@gmail.com>
18+
*/
19+
interface HashInterface
20+
{
21+
/**
22+
* Calculates the hash of a file.
23+
*
24+
* @param string $filepath
25+
*
26+
* @return string Returns a string containing the calculated message digest.
27+
*/
28+
public function hash($filepath);
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Yo! Symfony Resource Watcher.
5+
*
6+
* (c) YoSymfony <http://github.com/yosymfony>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spatie\PhpUnitWatcher\ResourceWatcher;
13+
14+
/**
15+
* Interface of a resource cache.
16+
*
17+
* @author Victor Puertas <vpgugr@gmail.com>
18+
*/
19+
interface ResourceCacheInterface
20+
{
21+
/**
22+
* If the cache Initialized? if not then warm-up cache.
23+
*
24+
* @return bool
25+
*/
26+
public function isInitialized();
27+
28+
/**
29+
* Returns the hash of a file in cache.
30+
*
31+
* @param string $filename
32+
*
33+
* @return string The hash for the filename. Empty string if not exists.
34+
*/
35+
public function read($filename);
36+
37+
/**
38+
* Updates the hash of a file in cache.
39+
*
40+
* @param string $filename
41+
* @param string $hash The calculated hash for the filename.
42+
*/
43+
public function write($filename, $hash);
44+
45+
/**
46+
* Deletes a file in cache.
47+
*
48+
* @param string $filename
49+
*
50+
* @return void
51+
*/
52+
public function delete($filename);
53+
54+
/**
55+
* Erases all the elements in cache.
56+
*
57+
* @return void
58+
*/
59+
public function erase();
60+
61+
/**
62+
* Returns all the element in cache.
63+
*
64+
* @return array A key-value array in which the key is the filename and the value is the hash.
65+
*/
66+
public function getAll();
67+
68+
/**
69+
* Persists the cache
70+
*
71+
* @return void
72+
*/
73+
public function save();
74+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Yo! Symfony Resource Watcher.
5+
*
6+
* (c) YoSymfony <http://github.com/yosymfony>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spatie\PhpUnitWatcher\ResourceWatcher;
13+
14+
/**
15+
* Resource cache implementation using memory.
16+
*
17+
* @author Victor Puertas <vpgugr@gmail.com>
18+
*/
19+
class ResourceCacheMemory implements ResourceCacheInterface
20+
{
21+
protected $isInitialized = false;
22+
private $data = [];
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function isInitialized()
28+
{
29+
return $this->isInitialized;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function read($filename)
36+
{
37+
return isset($this->data[$filename]) ? $this->data[$filename] : '';
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function write($filename, $hash)
44+
{
45+
$this->data[$filename] = $hash;
46+
$this->isInitialized = true;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function delete($filename)
53+
{
54+
unset($this->data[$filename]);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function erase()
61+
{
62+
$this->data = [];
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getAll()
69+
{
70+
return $this->data;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function save()
77+
{
78+
$this->isInitialized = true;
79+
}
80+
}

0 commit comments

Comments
 (0)