Skip to content

Commit dee308f

Browse files
author
Pierre Clavequin
committed
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
1 parent ff5ee6d commit dee308f

16 files changed

+1213
-3
lines changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@
2222
"symfony/console": "^5 | ^6 | ^7",
2323
"symfony/finder": "^5.4 | ^6 | ^7",
2424
"symfony/process": "^5.4 | ^6 | ^7",
25-
"symfony/yaml": "^5.2 | ^6 | ^7",
26-
"yosymfony/resource-watcher": "^2.0 | ^3.0"
25+
"symfony/yaml": "^5.2 | ^6 | ^7"
2726
},
2827
"conflict": {
29-
"yosymfony/resource-watcher": "<2.0",
3028
"symfony/console": "<5.2"
3129
},
3230
"require-dev": {
3331
"phpunit/phpunit": "^8.6 | ^9.0 | ^11.0"
3432
},
3533
"autoload": {
3634
"psr-4": {
35+
"Yosymfony\\": "src/ResourceWatcher",
3736
"Spatie\\PhpUnitWatcher\\": "src"
3837
}
3938
},

src/ResourceWatcher/.DS_Store

6 KB
Binary file not shown.
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 Yosymfony\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 Yosymfony\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 Yosymfony\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 Yosymfony\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 Yosymfony\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)