Giter Site home page Giter Site logo

php-hot-reloader's Introduction

PHP Hot Reloader

[UNMAINTAINED]: This repository is currently unmaintained, I've been very busy in the last months and im struggling to find some time to take care of this repo but with no success. I'm sorry for those who are using it. If you want to maintain this repo, please open an issue and let me know.

This is a class that adds a live reload feature to any php project. It allows you to see your page dynamically changing while coding, without have to keep refreshing the browser on every change.

This Reloader uses a SSE (server-sent events) to listen to changes and notify the browser. So, no built-in server, no custom port, no pain configuration needed.

Installing

Using Composer

composer require felippe-regazio/php-hot-reloader:dev-master --prefer-source

Manually

Clone this repository and copy its folder to somewhere on your project.

Configuring

  1. Copy the phrwatcher.php file on the repository root to somewhere on your app. This file must be available through some URL (you can change its name if you prefer), and if you visit this file you must see the message "SSE_ADDRESS_OK | PROJECT ROOT:" informing your project root. Copy this URL, you'll gonna need it.

  2. Open this file and configure the $variables according to your needings. Please, read the doc strings on the vars to properly configure them. Basically you will inform your app root on $PROJECT_ROOT, files to watch and files to ignore relative to the app root on $WATCH and $IGNORE.

  3. Now, lets imagine that you have configured your phrwatcher.php and it is available on the URL http://localhost/your-project/phrwatcher.php (the URL that you copied on the first step). Now, you must activate the reloader by calling it on your application and pointing it to this URL like this:

require_once "/php-hot-reloader/src/HotReloader.php";
new HotReloader\HotReloader('//localhost/your-project/phrwatcher.php');

If you installed this repository using composer, you dont need the require_once, just add the "autoloader" on your app and call the reloader.

So, you must pass the address that points to your phrwatcher.php file as the HotReloader() function param. You dont need to add the protocol, just add "//" as URL prefix. Is highly recommended to start the HotReloader on some front controller on your app. Also you should deactivate the reloader on production, never keep this feature active on production.

Git Integration

The $WATCH variable on the phrwatcher.php file defines the files and folders to track. So, instead of set a list of files and folder and observe them, you can Observe your repository. The Hot Reloader will use git to track the files currently being changed and watch them. This saves a lot of time and processing, since the files being tracked are updated automatically, and reserved only to the files that you are working on the moment. To track using GIT you must set your $WATCH variable like this:

$WATCH = "git:your/absolute/repository/path/";

If your phrfile.php is in your project root, you can use an automatic and dynamic version:

$WATCH = "git:" . __DIR__;

Now the Hot Reloader will automatically observe the files using your Git Status.

Tips

When working with larger projects, the diff algorithm can take some time to calc the modified files. To avoid the "input lag", you can follow this tips:

  1. On the $WATCH array of your phrwatcher.php try to keep only the necessary. If your project is huge, avoid using ".", and especify path by path, avoiding unecessary huge folders as vendors, tmp, log. Or consider use the Git Integration.

  2. The $IGNORE array is related only to what you are watching. If any vendor folder on the paths that you are watching, you must add them to your ignore list. Paths such as vendor, node_modules, sass_cache, tmp, log, keep them ignored.

  3. If your project is really big, is recomended to add the 'git' mode to your $WATCH list. So, your list will be always the result of a git status, consequently, you will be watching always the files that you are currently working on.

Example

There is an example file on the root of this repository. The example.php. Open this file on your browser to see the Hot Reloader working.

php-hot-reloader's People

Contributors

dharnarh avatar felippe-regazio avatar gagansandhu25 avatar omarlopesino avatar ttodua 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

php-hot-reloader's Issues

Can't make this work with Wordpress

Hi there!
I get an "Fatal error: Uncaught Error: Class..." when I try to use this in a wordpress environment. I've succeeded to require the php-file/class.
But as soon as I write this:
$reloader = new HotReloader(); I get the error message...

Can't figure out what I'm doing wrong...

Best, Niklas

Allow bootstrap variables to be overwritten from outside file

Do you think it might make sense to change the variable declarations in the phrwatcher.php bootstrap file:

$PROJECT_ROOT  = __DIR__;
$WATCH = ["."];
// etc.

to

$PROJECT_ROOT = $PROJECT_ROOT ?? __DIR__;
$WATCH = $WATCH ?? ["."];
// etc.

to allow optionally declaring these variables somewhere else?

I'm currently trying to require this plugin inside another plugin on a site I'm working on, and it would be great if I could keep the original files from this repo intact to run composer update without overwriting anything important.

Composer can not install your repo

Hello @felippe-regazio!

I ran the command compser require felippe-regazio/php-hot-reloader, but I received like:

composer require felippe-regazio/php-hot-reloader

                                                                                                                                                                                                                                      
  [InvalidArgumentException]                                                                                                                                                                                                          
  Could not find a matching version of package felippe-regazio/php-hot-reloader. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (stable).  
                                                                                                                                                                                                                                      

require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...

I also tried with these parameters, but nothing worked.

JS changes to the DOM cause a reload

Your code works perfectly for JS/CSS changes and for uploaded changes to the PHP/HTML.

However it also reloads (via the innerHTML replacement) even when a Javascript makes changes to the DOM, such as image sliders. This immediately erases those JS DOM changes, so breaks any JS generated elements.

Can this be fixed to only reload the innerHTML when the PHP/HTML has been changed by an upload, and not by a script?

[ideally it would need to re-fire those scripts after replacing with the new HTML too]

Reloading taking too long time

Hello i am using this package with codeigniter 3. It is working fine but it reloads page after 8-10 seconds. Is there anything that could make this time shorter.

Thanks a lot!

Warning: Cannot modify header information - headers already sent

Hello, first let me thank you for your work. It is very useful and I'm using your tool to teach my students.

I have the following issue where this warning appears on my page.
image
...and because of that, the page doesn't react to changes in linked files like CSS and JavaScript, but changes in PHP files still work.

The interesting part is that if I reduce the amount of output then the warning goes away and everything works OK.

My source code can be found here: https://github.com/nvbach91/4IZ278-2018-2019-LS/blob/master/www/nguv03/cv02/warning.php

I also tested this using plain html with no custom PHP at all and it seems like there is a certain amount of generated output at which the warning starts to show up.

I'm using PHP 7.3.1 in XAMPP

Thanks in advance!

Is it possible for this to work, in an includes file?

I put the required code in a includes file called header.php, other files in my project includes this file.
If I edit the header.php the hot reload works, however if I edit any of the other files that includes this file nothing happens. Is there any way I can get this to work on other files as well that uses header.php, without having to copy and paste the code into each file?

Infinite loop?

I tried to use this on an older (large) project and it seemed like it conflicted with some jQuery code or other libraries (not sure). I was using it on a local dev server (vagrant) and the console was showing hundreds of requests per second (reloading the page?) and some errors.

It seemed to sort of work, but after the first reload it just stopped working because the server was overloaded from refreshing every single included file non-stop.

Edit: It might be because this requires ES6 javascript and my project is not ES6.

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.