Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ jobs:
uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main
cs:
uses: innmind/github-workflows/.github/workflows/cs.yml@main
with:
php-version: '8.2'
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Changed

- Requires PHP `8.4`
- Requires `innmind/server-control:~7.0`

## 5.0.0 - 2025-05-08

### Changed
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FileWatch

[![Build Status](https://github.com/innmind/filewatch/workflows/CI/badge.svg?branch=master)](https://github.com/innmind/filewatch/actions?query=workflow%3ACI)
[![CI](https://github.com/Innmind/FileWatch/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Innmind/FileWatch/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/innmind/filewatch/branch/develop/graph/badge.svg)](https://codecov.io/gh/innmind/filewatch)
[![Type Coverage](https://shepherd.dev/github/innmind/filewatch/coverage.svg)](https://shepherd.dev/github/innmind/filewatch)

Expand All @@ -15,12 +15,9 @@ composer require innmind/file-watch
## Usage

```php
use Innmind\FileWatch\{
Factory,
Stop,
};
use Innmind\FileWatch\Factory;
use Innmind\Server\Control\ServerFactory;
use Innmind\TimeWarp\Halt\Usleep;
use Innmind\TimeWarp\Halt;
use Innmind\TimeContinuum\Clock;
use Innmind\IO\IO;
use Innmind\Url\Path;
Expand All @@ -30,9 +27,9 @@ $watch = Factory::build(
ServerFactory::build(
Clock::live(),
IO::fromAmbientAuthority(),
Usleep::new(),
Halt::new(),
)->processes(),
Usleep::new(),
Halt::new(),
);

$count = $watch(Path::of('/to/some/file/or/folder'))(0, function(int $count, Continuation $continuation): Continuation {
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"issues": "http://github.com/Innmind/FileWatch/issues"
},
"require": {
"php": "~8.2",
"innmind/url": "~4.0",
"innmind/server-control": "~6.0",
"innmind/time-warp": "~4.0",
"innmind/time-continuum": "^4.1.1",
"php": "~8.4",
"innmind/url": "~5.0",
"innmind/server-control": "~7.0",
"innmind/time-warp": "~5.1",
"innmind/time-continuum": "~5.1",
"psr/log": "~3.0"
},
"autoload": {
Expand All @@ -33,7 +33,7 @@
}
},
"require-dev": {
"innmind/static-analysis": "^1.2.1",
"innmind/static-analysis": "~1.3",
"innmind/coding-standard": "~2.0",
"innmind/black-box": "~6.1"
}
Expand Down
6 changes: 3 additions & 3 deletions src/Ping/OutputDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __invoke(mixed $carry, callable $ping): Attempt
$result = Sequence::lazy(function() {
while (true) {
yield $this->output();
($this->halt)($this->period);
$_ = ($this->halt)($this->period)->memoize();
}
})
->flatMap(static fn($maybe) => $maybe->match(
Expand Down Expand Up @@ -136,11 +136,11 @@ private function diff(Sequence $previous, Sequence $now): bool
{
$previous = $previous
->map(static fn($chunk) => $chunk->data())
->fold(new Concat)
->fold(Concat::monoid)
->toString();
$now = $now
->map(static fn($chunk) => $chunk->data())
->fold(new Concat)
->fold(Concat::monoid)
->toString();

return $previous !== $now;
Expand Down
3 changes: 2 additions & 1 deletion src/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Innmind\FileWatch;

use Innmind\FileWatch\Watch\{
Implementation,
Kind,
Logger,
Fallback,
Expand All @@ -19,7 +20,7 @@
final class Watch
{
private function __construct(
private Kind|Logger $implementation,
private Implementation $implementation,
) {
}

Expand Down
15 changes: 15 additions & 0 deletions src/Watch/Implementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types = 1);

namespace Innmind\FileWatch\Watch;

use Innmind\FileWatch\Ping;
use Innmind\Url\Path;

/**
* @internal
*/
interface Implementation
{
public function __invoke(Path $file): Ping\Implementation;
}
3 changes: 2 additions & 1 deletion src/Watch/Kind.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
/**
* @internal
*/
final class Kind
final class Kind implements Implementation
{
public function __construct(
private Fallback $files,
private Stat $directories,
) {
}

#[\Override]
public function __invoke(Path $file): Ping\Implementation
{
return match ($file->directory()) {
Expand Down
18 changes: 5 additions & 13 deletions src/Watch/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
/**
* @internal
*/
final class Logger
final class Logger implements Implementation
{
private function __construct(
private Kind $watch,
private Implementation $watch,
private LoggerInterface $logger,
) {
}

#[\Override]
public function __invoke(Path $path): Ping\Implementation
{
return Ping\Logger::psr(
Expand All @@ -27,17 +28,8 @@ public function __invoke(Path $path): Ping\Implementation
);
}

public static function psr(Kind|self $watch, LoggerInterface $logger): self
public static function psr(Implementation $watch, LoggerInterface $logger): self
{
return new self(self::extract($watch), $logger);
}

private static function extract(Kind|self $watch): Kind
{
if ($watch instanceof Kind) {
return $watch;
}

return self::extract($watch->watch);
return new self($watch, $logger);
}
}
42 changes: 21 additions & 21 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
Factory,
Watch,
};
use Innmind\Server\Control\Server\{
Processes\Unix,
Command,
use Innmind\Server\Control\{
ServerFactory,
Server\Command,
};
use Innmind\TimeContinuum\Clock;
use Innmind\TimeWarp\Halt\Usleep;
use Innmind\TimeWarp\Halt;
use Innmind\IO\IO;
use Innmind\Url\Path;
use Psr\Log\NullLogger;
Expand All @@ -34,16 +34,16 @@ public function tearDown(): void
public function testWatchFile()
{
\touch('/tmp/innmind/watch-file');
$processes = Unix::of(
$processes = ServerFactory::build(
Clock::live(),
IO::fromAmbientAuthority(),
Usleep::new(),
);
Halt::new(),
)->processes();
$process = $processes->execute(Command::background(
'sleep 1 && echo foo >> /tmp/innmind/watch-file && sleep 1 && echo foo >> /tmp/innmind/watch-file && sleep 1 && echo foo >> /tmp/innmind/watch-file',
));

$watch = Factory::build($processes, Usleep::new());
$watch = Factory::build($processes, Halt::new());

$either = $watch(Path::of('/tmp/innmind/watch-file'))(0, static function($count, $continuation) {
++$count;
Expand All @@ -66,16 +66,16 @@ public function testWatchFile()

public function testWatchDirectory()
{
$processes = Unix::of(
$processes = ServerFactory::build(
Clock::live(),
IO::fromAmbientAuthority(),
Usleep::new(),
);
Halt::new(),
)->processes();
$process = $processes->execute(Command::background(
'sleep 1 && touch /tmp/innmind/watch-file && sleep 1 && rm /tmp/innmind/watch-file',
));

$watch = Factory::build($processes, Usleep::new());
$watch = Factory::build($processes, Halt::new());

$either = $watch(Path::of('/tmp/innmind/'))(0, static function($count, $continuation) {
++$count;
Expand All @@ -98,13 +98,13 @@ public function testWatchDirectory()

public function testReturnErrorWhenWatchingUnknownFile()
{
$processes = Unix::of(
$processes = ServerFactory::build(
Clock::live(),
IO::fromAmbientAuthority(),
Usleep::new(),
);
Halt::new(),
)->processes();

$watch = Factory::build($processes, Usleep::new());
$watch = Factory::build($processes, Halt::new());

$either = $watch(Path::of('/unknown/'))(null, static fn($_, $continuation) => $continuation);

Expand All @@ -118,16 +118,16 @@ public function testReturnErrorWhenWatchingUnknownFile()
public function testLog()
{
\touch('/tmp/innmind/watch-file');
$processes = Unix::of(
$processes = ServerFactory::build(
Clock::live(),
IO::fromAmbientAuthority(),
Usleep::new(),
);
Halt::new(),
)->processes();
$process = $processes->execute(Command::background(
'sleep 1 && echo foo >> /tmp/innmind/watch-file && sleep 1 && echo foo >> /tmp/innmind/watch-file && sleep 1 && echo foo >> /tmp/innmind/watch-file',
));
))->unwrap();

$inner = Factory::build($processes, Usleep::new());
$inner = Factory::build($processes, Halt::new());
$watch = Watch::logger($inner, new NullLogger);

$either = $watch(Path::of('/tmp/innmind/watch-file'))(0, static function($count, $continuation) {
Expand Down