0

There are so many preprocessing or compiling languages, each with its own learning curve e.g. (scss, sass, jade,typescript ) and the number seems not to be reducing.

Wouldn't it be cool if developers could execute PHP to produce any text document just like how the above are compiled to native (html, css, js) using file watchers .

Advantages

  1. No additional learning curve.
  2. One Language.
  3. Reduce repetitive coding.
  4. Generate your code in less time with fewer bugs
  5. Produce consistent code that adheres to your standards.
  6. more time focus on planning our application.

WHAT HAVE I TRIED

By editing Jade Source Code and changing the extension from html to php.

File:       jade.js

Location:   npm\node_modules\jade\bin\

Line:       249

I have been able to execute Jade file to PHP

enter image description here

HOW I THINK THIIS CAN BE ACHIEVED

there could be multiple of these transpilers.

template-css -> compiles to css

template-php -> compiles to php

template-js -> compiles to javascript.

and so on.

The Middle Parser or file watcher will do mainly 3 task

  1. Replace the file extension from template-filetye to php.

  2. Executes the new file.php against the PHP executable or parser essential just like running inside a browser.

  3. Return to the file watcher the text specified.

This is the code in the jade.cmd file

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\jade\bin\jade.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "%~dp0\node_modules\jade\bin\jade.js" %*
)

Note: I don’t understand the cmd file. I have not modified this file but taught it might be helpful to any one who wants to help.

Inspiration

http://www.codesmithtools.com/product/generator

Links that MIGHT be helpful

http://php.net/manual/en/function.exec.php

http://php.net/manual/it/install.windows.commandline.php

Any Help in building the filewatcher or whatever the appropriate name is will be greatly appreciated.

Koofah
  • 79
  • 1
  • 7

1 Answers1

1

If you're looking only for a way to watch and execute php files from IDE when they change then you already have all that's needed.

Take a look at this file watcher in PhpStorm

This file watcher will run $PhpExecutable$ which in my case translates to /usr/lib/php every time a file in Working directory changes and put STDOUT to a file within the same directory but with new extension .txt

IDE will automatically group input and output file so they're easier to manage.

If you need something more advanced you always could write own script that could be run instead of $PhpExecutable$ and use php output control

For example:

#!/usr/bin/env php
<?php

ob_start();
// ... preprocess? init some variables?
include $argv[1];
$output = ob_get_clean();
// ... postprocess $output?
echo $output; // echo to STDOUT, or to file with file_put_contents(preg_replace('/\.php$/', '.txt', $argv[1]), $output);
Michal Bieda
  • 954
  • 5
  • 13
  • I am getting invalid executable. – Koofah Jun 23 '16 at 06:16
  • @Koofah sounds like it can't find `php` executable, be sure you have it in your system path. If it fails you can enter absolute path for `php` (in my case `/usr/bin/php` on Ubuntu, can be different on Macs, other linux systems or Windows) – Michal Bieda Jun 23 '16 at 06:27
  • Thanks It works as expected i defined the full path to the executable. i have 2 questions. 1. in which extension should i save the custom script. 2. IN the custom script, i would want to execute e.g. file.template-txt to .txt and file2.template-css to .css – Koofah Jun 23 '16 at 06:34
  • i don't know the extension to save the custom file in. i keep getting occurred occured while executing watcher' PHP Executable" cannot run the file. error = 193, %1 is not a valid win 32 application. – Koofah Jun 23 '16 at 07:49
  • On windows you should set `Program` to `php` executable and put your custom script in the `Arguments` field so it will look something like this: `myCustomScript.php $FilePath$` – Michal Bieda Jun 23 '16 at 08:13
  • it works . Thanks. Wish a lot of developers could be using this to write multiple files. I am currently using it to write my html, css js and even php file with just PHP. Templating rocks. – Koofah Jun 23 '16 at 12:34
  • it is possible to change the Output paths to refresh into for e.g template-php to .php , template-css to .css using regular expression? FilenameWithoutExtension. ($fileExtension) without the template dynamically?. Sounds a little over stretch but is it possible? – Koofah Jun 23 '16 at 12:41
  • it does not fold like directory structures since i am using the file put content technique which outputs to a new file – Koofah Jun 23 '16 at 12:57