change working directory from cli php script - php

I wrote a simple project boostraper using bash.
It asks simple questions such as the parent folder to create the project inside, the type of the project (cli, web php, web symfony, web npm, etc…), the required dependencies, bootstrap everything, init git, and so on.
Then it returns the terminal to the user after changing the working directory to the freshly bootstraped one.
Fine.
I decided to rewrite the whole script using php in order to have a more user friendly cli interface (nice menus, progress bars, etc…) and to be able to pack the whole script in one and only binary (phar).
I struggle with the last part of the script: The one which was the simpliest part of the previous bash script:
cd $newprojectpath
exit 0;
For a reason I can't really figure out, I cannot do the same using a php script.
chdir allows me to change the working directory for the current php process but as soon as it is over, I'm back to working directory from where I invoked the php script (the one from where I spawned the php interpreter sub-process, obviously).
The only way I found in order to achieve this very simple move is to spawn a new shell sub-process from the php script itself.
It works, but I don't like it.
chdir($project->path);
pcntl_exec(getenv('SHELL'));
Did I miss a more simple way to change working directory using PHP cli ?

Related

Using gulp livereload with XAMPP

I been using gulp for front-end development lately and I find it very helpful.
I use XAMPP in windows for making PHP website some of which sometime include database operations.
Now, I have used gulp-livereload and gulp-connect for starting a server for front-end but then it won't process the PHP files.
All I want to do is, PHP livereload with database access. Like livereloading but via XAMPP's server(since it can process PHP).
yes, I have found a temporary solution for now.
See, it's just temporary but as long as you write PHP correct, you will get the work done. Let me tell you how I do it.
Though right off the start I want to tell you that this is not a proper way to approach this, you should use some MVC framework.
Anyway,
I have XAMPP installed under Windows 8.1 and XAMPP's htdocs is in my "C" Drive.
I then create a folder inside "htdocs" in which I put all my frontend + backend code.Basically, gulpfile.js is at the root, then there is a components folder in which all front-end sources reside. There is another folder at root called "www" inside which I put my index.php.
Then I load gulp-livereload instead of gulp-connect and I add the livereload.listen(); method and Tags in all the php files I want to reload.
Works very fine except when PHP throws an error, untill you correct that error, you have keep reloading the page manually.

in php get the files linked from each other

I have a website with index.php. I am using linux
There are several other php files where the index.php file connects to for some functions.
How to get a complete list of all the php files that are called, and whatever times they are called.
How can I see the complete flow chart how the code is running. (I mean which are functions are being called from which php file)
I want to do it through linux command line. I dont want to install any ide. Presently i have vim with vdebug plugin. BUt in that i have to step into the entire code which is very time taking.

Running a git post-commit hook

I'm running MAMP locally. I have a php script that I use to build a web site, to generate static HTML files, that I then push to a web server.
This process acts a lot like Jekyll on github pages.
For now, I simply make changes to Markdown files (my content) and then hit a local url, localhost/mysite/build, to which generates the html files. Simple.
I do have git installed locally to version the PHP script itself, along with the markdown files.
Ideally I would like to create a post-commit hook that will simply "ping" that build URL to allow it do run.
A couple things. I have been trying but the post-commit doesn't run the URL.
I would like to use PHP in the post-commit file, is that possible? I don't need any validation or anything yet, just simply want to call the URL to have the process run when I commit.
I have done the chmod to make sure the script is executable.
The post-commit file is named 'post-commit', no extension.
I would think I could just add one simple line, like a file_get_contents(myurl), or something close to that.
I have been googling for a few hours now and found things that are close but not exactly right. Its really very simple, just sort of a noob with git hooks.
Thanks.
Commit hooks are shell scripts, so it is definitely possible to either run a php script (just as you would do from cmd line) or issue a request to your URL using curl: curl --request GET --url http://stackoverflow.com -v in order to fire the build. Depending on the build time you might want to run curl in the background.
First of all, try to prepare a shell script, which runs the build. Then setup the hook itself. See Simple git post-commit hook to copy committed files to a certain folder as an example hook.

Netbeans PHP - how to run all the files in a directory

I am running PHP files in my NetBeans with a shell script command. So far so good. I can run individual files without any problem. Sometimes, however, I would like to run all the files in a directory (and it's subdirectories). I tried selecting the directory and hitting the shortcut for "Run" but nothing happens. If I select multiple files and hit "Run" only the last one selected will be run.
I am using the latest NetBeans 7.0.1 on MacOS 10.6. Thanks.
Never heard of such a feature. What I'd do with NetBeans is create a php application and in the main file write some code calling each php file... it will look ugly but I think it's the only way

How to run a .php file over and over...cron perhaps?

I have a php file that calls exec() on a c++ exe.When the .exe is finishing running I need to run the php file again,repeat .
I am wondering what is the best way to do this? This .php file has no user interaction and needs to be completely automated.
EDIT:Found another solution that would kill the process if it is already running which will cover this issue well.See posts on this page.
http://php.net/manual/en/function.getmypid.php
Here's a simple Linux line that starts up the script in the background and uses the watch command to restart the script whenever it finishes running:
watch -n 0 php /path/to/script.php &
Once you've started it like this, you can use the ps command to list running processes and find the process ID for watch, and then use kill process_id to stop watch.
It's probably not best practice to run commands like this, but it's a quick and easy solution that doesn't require any special access privileges on the system (whereas using cron might), and doesn't involve editing your code (whereas a codeigniter solution will).
I haven't used codeigniter before but there seems to be a solution as described in the wiki.
Depending on how you can access the system (if you are admin or not) and depending on how you are planning to update the automated commands, IMHO, you could use both solution (Linux crontab or codeigniter cron script).

Categories