Giter Site home page Giter Site logo

seregazhuk / php-watcher Goto Github PK

View Code? Open in Web Editor NEW
392.0 18.0 29.0 1.01 MB

Monitor for any changes in your php application and automatically restart it (suitable for async apps).

Home Page: https://sergeyzhuk.me/2019/10/18/php-watcher/

PHP 100.00%
php php-watcher reactphp async asynchronous cli watch

php-watcher's Introduction

PHP-watcher

PHP Version Build Status Total Downloads

Latest Stable Version Latest Version Latest Unstable Version

License

watcher logo

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:

watcher screenshot

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

You can install this package globally like this:

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:

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:

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:

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 <file> 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:

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:

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:

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:

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

Or specific files can be ignored:

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

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

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:

php-watcher server.php --delay 10 

For more precision, use a float:

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.

executable: php

or using CLI:

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:

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.

php-watcher --signal SIGTERM server.php

Your application can handle the signal as follows:

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.

app exit

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':

php-watcher server.php --no-spinner

License

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

How can I thank you?

Why not star this GitHub repo? I'd love the attention! Or, you can donate to my project on PayPal:

Support me with some coffee

Thanks!

php-watcher's People

Contributors

gorbunov avatar kpicaza avatar mmoreram avatar seregazhuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-watcher's Issues

Make it compatible with Symfony 5

Some required libraries are blocked to Symfony^4, blocking then all project requiring this watcher. We could make some push to them by adding some PRs, or just checking how to uncouple from them.

Add ability to provide custom signal to a running application

Currently, when the watcher restarts the script it always sends SIGTERM. We need to customize it and allow it to send any signal. Make it customizable via config signal:

watch:
  - src
  - config
signal: SIGINT

or in a command line:

php-watcher server.php --signal SIGINT

Bug: Readme typo

Probably a small typo in the first line of readme file by automatically them...

High CPU usage

It works, it does its job, it's not based on node so I can stick to PHP when developing with PHP.

However, it seems to have a quite significant CPU (I/O?) usage.

Is there something that could be done to reduce that?

Dockerized run loses STDERR

When script run within docker container, docker internally catches STDERR from script run as entrypoint, to allow capturing error logs via docker log command, and then copies STDERR to STDOUT. When entryscript is php-watcher, docker unable to capture STDERR, so I unable to see any errors in the console or with docker log command.
Running with --arguments 2>&1 helps.

Implement custom spinner

We need to decouple from alecrabbit/php-console-spinner. It has a lot of dependencies, for example under the hood it requires laravel container 6.0 and thus cannot be installed on the latest laravel version.

The logic to render a spinner is pretty simple and can be easily implemented right here inside the library.

Still maintaining?

I was trying to update a project that uses this tool, unfortunately, it seems to be no longer maintained and doesn't support PHP8.

Can we consider this project dead since the last release was already in 2019 and the last commit was also in 2021?

If this project is no longer maintained, can we inform new visitors in the README that it is no longer supported, and recommend some alternatives (if any) simultaneously?

[Feature] Make it truly asynchronous

Now, file changes detections run synchronously and thus it blocks the event loop. The idea is to create a child process. Inside we run the script which endlessly checks the changes.

Compatibility with Symfony5

Bug Report

root@1b51a4eb9dd9:/app/symfony/app# php composer.phar require seregazhuk/php-watcher --dev
Cannot load Xdebug - it was already loaded
Using version ^0.5.2 for seregazhuk/php-watcher
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "5.0.*"
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - yosymfony/resource-watcher v2.0.0 requires symfony/finder ^2.7|^3.0|^4.0 -> no matching package found.
    - seregazhuk/php-watcher v0.5.2 requires yosymfony/resource-watcher ^2.0 -> satisfiable by yosymfony/resource-watcher[v2.0.0].
    - Installation request for seregazhuk/php-watcher ^0.5.2 -> satisfiable by seregazhuk/php-watcher[v0.5.2].

Potential causes:
 - A typo in the package name
 - The package is not available in a stable-enough version according to your minimum-stability setting
   see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
 - It's a private package and you forgot to add a custom repository to find it

Input Code

require seregazhuk/php-watcher --dev

Expected behavior

install the library

Possible Solution

get rid of yosymfony/resource-watcher dependency

Option to disable spinner

Would be nice to make option to run this without spinner, as docker console looks really nice, but become unhelpful )
Снимок экрана 2019-10-25 в 12 15 20

Console colours

Console colours don't seem to work, am I doing something wrong?

This is the output I'm getting:

Screen Shot 2020-02-27 at 13 29 43

It should be this:

Screen Shot 2020-02-27 at 13 29 52

php-watcher has unmet requirements

I'm trying to install php-watcher within latest composer docker with composer require seregazhuk/php-watcher command, but getting error:

Package seregazhuk/php-watcher has a PHP requirement incompatible with your PHP version, PHP extensions and Composer version

Docker container has Composer version 2.0.11 2021-02-24 14:57:23 and PHP 8.0.3 (cli) (built: Mar 6 2021 03:28:33) ( NTS ).

I'm new to PHP and not sure what composer require --ignore-platform-reqs seregazhuk/php-watcher is doing, but after that ./vendor/bin/php-watcher app.php gives:

Fatal error: Uncaught Error: Undefined constant "seregazhuk\PhpWatcher\Config\SIGINT" in /app/vendor/seregazhuk/php-watcher/src/Config/Config.php:40
Stack trace:
#0 /app/vendor/seregazhuk/php-watcher/src/Config/Builder.php(21): seregazhuk\PhpWatcher\Config\Config::fromArray(Array)
#1 /app/vendor/seregazhuk/php-watcher/src/WatcherCommand.php(73): seregazhuk\PhpWatcher\Config\Builder->fromConfigFile('')
#2 /app/vendor/seregazhuk/php-watcher/src/WatcherCommand.php(42): seregazhuk\PhpWatcher\WatcherCommand->buildConfig(Object(Symfony\Component\Console\Input\ArgvInput))
#3 /app/vendor/symfony/console/Command/Command.php(256): seregazhuk\PhpWatcher\WatcherCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /app/vendor/symfony/console/Application.php(971): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /app/vendor/symfony/console/Application.php(290): Symfony\Component\Console\Application->doRunCommand(Object(seregazhuk\PhpWatcher\WatcherCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 /app/vendor/symfony/console/Application.php(166): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /app/vendor/seregazhuk/php-watcher/php-watcher(18): Symfony\Component\Console\Application->run()

And now composer check-platform-reqs outputs:

Checking platform requirements for packages in the vendor dir
ext-curl      8.0.3                                                 success  
ext-json      8.0.3                                                 success  
ext-mbstring  8.0.3                                                 success  
ext-pcntl     n/a    seregazhuk/php-watcher requires ext-pcntl (*)  missing  
php           8.0.3  seregazhuk/php-watcher requires php (^7.2)     failed   

Is there a way I could use php-watcher in such setup?

Laravel Octane appears to be restarted twice

Hi, thanks for this project 👋

I am trying to use this project as a lightweight alternative to chokidar for restarting Laravel Octane: https://laravel.com/docs/8.x/octane#watching-for-file-changes

When updating a watched file, it appears the process is restarted twice:

www-data@nemo-api:~$ vendor/bin/php-watcher artisan --arguments=octane:start --watch=app

[PHP-Watcher] 0.5.2
[PHP-Watcher] watching: app
[PHP-Watcher] starting `php artisan octane:start`
⠛
   INFO  Server running…

  Local: http://127.0.0.1:8000

  Press Ctrl+C to stop the server


[PHP-Watcher] restarting due to changes...
[PHP-Watcher] starting `php artisan octane:start`
[PHP-Watcher] starting `php artisan octane:start`

In Process.php line 170:
                              
  Process is already running  
                              

watch [-w|--watch [WATCH]] [-e|--ext [EXT]] [-i|--ignore [IGNORE]] [--exec [EXEC]] [--delay [DELAY]] [--signal [SIGNAL]] [--arguments [ARGUMENTS]] [--config [CONFIG]] [--no-spinner] [--] <script>

If you need to reproduce this locally, a plain Laravel install with Octane should do:

Improve spinner output

Work directly with a cursor instead of using "\r".
When reloading a script we should hide the spinner, otherwise part of it stays in the terminal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.