百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

php-watcher

> 前端框架
开源

监控 php 应用程序中的任何变化,并自动重新启动它(适用于异步应用程序)。

394 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

监控 php 应用程序中的任何变化,并自动重新启动它(适用于异步应用程序)。

PHP-watcher

PHP-watcher helps develop long-running PHP applications by automatically restarting them when file changes in the directory are detected.

Here's how it looks like:

PHP-watcher does not require any additional changes to your code or method of development. php-watcher is a replacement wrapper for php, to use PHP -watcher replace the word php in the command line when executing your script.

Table of contents

  • Installation
  • Usage
  • Config files
  • Monitoring multiple directories
  • Specifying extension watch list
  • Ignoring files
  • Delaying restarting
  • Default executable
  • Gracefully reloading down your script
  • Automatic restart
  • Performance
  • Spinner

Installation

You can install this package globally like this:

bash
composer global require seregazhuk/php-watcher

After that phpunit-watcher watch can be run in any directory on your system.

Alternatively, you can install the package locally as a dev dependency in your project:

bash
composer require seregazhuk/php-watcher --dev

Locally installed you can run it with vendor/bin/php-watcher.

Usage

All the examples assume you've installed the package globally. If you opted for the local installation prepend vendor/bin/ everywhere where php-watcher is mentioned.

PHP-watcher wraps your application, so you can pass all the arguments you would normally pass to your app:

bash
php-watcher [your php app]

Using PHP-Watcher is simple. If your application accepts a host and port as the arguments, I can start it using option --arguments:

bash
php-watcher server.php --arguments localhost --arguments 8080

Any output from this script is prefixed with [php-watcher], otherwise all output from your application, errors included, will be echoed out as expected.

Config files

PHP-Watcher supports customization of its behavior with config files. The file for options may be named .php-watcher.yml, php-watcher.yml or php-watcher.yml.dist. The tool will look for a file in the current working directory in that order. An alternative local configuration file can be specified with the --config option.

The specificity is as follows, so that a command line argument will always override the config file settings:

  • command line arguments
  • local config

A config file can take any of the command line arguments, for example:

yaml
watch:
  - src
  - config
extensions:
  - php
  - yml
ignore:
  - tests

Monitoring multiple directories

By default, PHP-Watcher monitors the current working directory. If you want to take control of that option, use the --watch option to add specific paths:

bash
php-watcher --watch src --watch config server.php

Now PHP-Watcher will only restart if there are changes in the ./src or ./config directories. By default traverses sub-directories, so there's no need to explicitly include them.

Specifying extension watch list

By default, PHP-Watcher looks for files with the .php extension. If you use the --ext option and monitor app,yml PHP-Watcher will monitor files with the extension of .php and .yml:

bash
php-watcher server.php --ext=php,yml

Now PHP-Watcher will restart on any changes to files in the directory (or subdirectories) with the extensions .php, .yml.

Ignoring files

By default, PHP-Watcher will only restart when a .php file changes. In some cases you may want to ignore some specific files, directories or file patterns, to prevent PHP-Watcher from prematurely restarting your application.

This can be done via the command line:

bash
php-watcher server.php --ignore public/ --ignore tests/

Or specific files can be ignored:

bash
php-watcher server.php --ignore src/config.php

Patterns can also be ignored (but be sure to quote the arguments):

bash
php-watcher server.php --ignore 'src/config/*.php'

Note that by default, PHP-Watcher ignores all dot and VCS files.

Delaying restarting

In some situations, you may want to wait until a number of files have changed . The timeout before checking for new file changes is 1 second. If you're uploading a number of files and it's taking some number of seconds, this could cause your app to restart multiple times unnecessarily.

To add an extra throttle, or delay restarting, use the --delay option:

bash
php-watcher server.php --delay 10 

For more precision, use a float:

bash
php-watcher server.php --delay 2.5 

Default executable

By default, PHP-Watcher uses php bin executable to run your scripts. If you want to provide your own executable use --exec option or executable param in config file. This is particularly useful if you're working with several PHP versions.

yaml
executable: php

or using CLI:

bash
php-watcher server.php --exec php7

Running non-php scripts

PHP-Watcher can also be used to execute and monitor other non-php programs. For example, you can use PHP-Watcher to listen to *.js files and use node executable to run them:

bash
php-watcher server.js --exec node --watch app --ext=js

The command above uses NodeJS to start server.js and then listens to changes in app directory.

Gracefully reloading down your script

It is possible to have PHP-watcher send any signal that you specify to your application.

bash
php-watcher --signal SIGTERM server.php

Your application can handle the signal as follows:

php
declare(ticks = 1);

pcntl_signal(SIGTERM, 'terminationHandler');

function terminationHandler()
{
    // ...        
}

By default PHP-watcher sends SIGINT signal.

Automatic restart

PHP-watcher was originally written to restart long-running processes such as web servers, but it also supports apps that cleanly exit. If your script exits cleanly, the watcher will continue to monitor the directory (or directories) and restart the script if there are any changes. If the script crashes PHP-watcher will notify you about that.

Performance

The watcher can use different strategies to monitor your file system changes. Under the hood it detects the environment and chooses the best suitable strategy.

Fswatch (OSX only)

FsWatch is a cross-platform (Linux,Mac,Windows) file change monitor that will automatically use the platforms native functionality when possible. Under the hood the filesystem notifies us when any changes occur. Currently, it doesn't work correctly on Linux. If your system is OSx and has fswatch installed, this strategy will be used.

Has not been extensively tested.

Chokidar

Chokidar is a JavaScript package for watching file and directory changes. At first run the watcher will check if Node.js is available in the system. If it is, it will install chokidar into the project.

Spinner

By default, the watcher outputs a nice spinner which indicates that the process is running and watching your files. But if your system doesn't support ansi coded the watcher will try to detect it and disable the spinner. Or you can always disable the spinner manually with option '--no-spinner':

bash
php-watcher server.php --no-spinner

License

MIT http://rem.mit-license.org

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPasyncasynchronouscliphp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架