I want to program a little CLI script in PHP, basically with two possible arguments to do two different things. Very easy. But I would like to do in an elegant way.
I was wondering if it exists some PHP micro-framework functionaly in CLI mode (note that I say micro-framework and not framework). I want to use PHP because I'll include some PHP already programmed classes.
I have found CLImax, that is an specific CLI micro-framework and seems good, but it lacks a good documentation.
Do you know any place where I can found a good CLImax documentation (I haven't found anything, but its source code poorly documented)?
Or maybe do you know another option? Alloy seems as well a lightweight PHP framework, and has CLI "mode", but I don't know if it's too generic, as it's as well for web servers. Have you used it?
Check out the Symfony Console component. Here's an introduction to using it. It may take a little bit of work to get it to function with an older version of PHP (without namespaces), and you need a couple other components from Symfony, but I've used it quite successfully.
I've found this one: php-cli-tools
It's definitely micro and brings everything you'd have with sh or windows batch.
It doesn't have great documentation from what I can see, but Cilex describes itself as:-
"a lightweight framework for creating PHP CLI scripts inspired by Silex"
Might be worth a look, I am thinking of using it myself to replace the bash deployment scripts I have written.
Laravel has a command line tool called Artisan which it allows you to extend quite easily;
Artisan is the name of the command-line interface included with Laravel.... [Artisan Development] In addition to the commands provided with Artisan, you may also build your own custom commands for working with your application.
See the documentation for Artisan development here
Check out https://github.com/c9s/CLIFramework
CLIFramework is a full-stack framework for command-line tools, unlike Symfony/Console, it has a concise API for building lightweight command-line application.
It allows you to define few options and argument info to generate a help document rapidly.
The most important part is that CLIFramework can generate zsh completion script automatically, so you don't need to write zsh script for every command line application.
You might be interested in my php cli framework: https://github.com/b-b3rn4rd/Terminalor
Terminalor - is a php library for creating portable php cli files. It provides essential functionality to work under cli interface and can be used as a wrapper for external libraries. It allows rapidly create documented cli commands with arguments using closures and phpdoc comments. Later this commands can be compiled into single independent portable file.
I don' think many of the suggestions above are micro-frameworks - even though many of the suggestions are great frameworks - and properbly better in many use-cases than my suggestion: https://github.com/diversen/minimal-cli-framework
It is much faster than the above frameworks (though it is rare that you need speed from a CLI-framework). It lets you add sub-commands using class objects. It generates help and command-definitions from a single method. You will only need to implement two methods to an existing class to use it with the minimal-cli-framework (getCommand - definition of the command and runCommand - the execution of the command).
Disclaminer: I wrote the package.
Did you try Seagull? It looks like it too natively supports projects that are specifically meant to be used in the CLI.
Related
I really appreciate Python's nose utility, which is a testing framework extension that watches your source code for changes and runs the unittests anytime something is modified.
I was wondering whether anything such as this existed for PHP / as a PHPUnit extension? I'm fully aware that this sort of thing wouldn't be difficult to conjure up in bash using watch, but I work across platforms and would ideally like to avoid writing separate implementations.
You can use watchr for this.
PHPUnit Watchr config
Another PHPUnit Watchr config
Guide on how to combine Watchr, Growl and PHPUnit
I have developed a PHP web application, but a client insists on a real Windows application, since he doesn't like running the software inside a browser.
Are there any solutions for this, any compilers to turn a web project into a Windows exe ?
I have looked at Phc-Win , but that seems more suited for small command line utils, not for entire web-applications...
UPDATE:
just found this myself, both look quite promising...
http://www.zzee.com/phpexe/
http://www.exeoutput.com/index.php
There's no tool for this, short of a simple wrapper app that embeds a browser inside an otherwise normal application window. Your PHP app would have to be completely re-written to include ALL of the overhead code necessary to build a GUI - basically all the 'display' stuff that a browser does automatically, would have to added to your app.
Well some of you did not google good enough:
http://www.appcelerator.com/products/titanium-cross-platform-application-development/
Supports most of the "web-languages" to write native Applications.
Tutorial Reference for Appcelerator: http://appceleratortitanium.com/tutorials/3-appcelerator-titanium-tutorial-beginners.html
Quote:
"Q: What languages can I use to make desktop apps?
A: Javascript, PHP, Python, Ruby, HTML, HTML5, and CSS."
Not exactly what you are looking for: http://www.roadsend.co/home/index.php?pageID=compiler
You may look into "HipHop" (made by Facebook): Converts PHP to compileable C++-
There's a tool for this. :-) I never used it but you can try this: Winbinder.
It is simple to compile your PHP source code into an executable. Facebook released a compiler for PHP in early-2010, called HipHop, which aims to create C++. You could then compile this code, for example using gcc, to machine-code.
The more difficult point for a complex Web-App like yours is the user interface. When compiling the way I described above, the application can be run from command line - this might work for simple tasks, but not, if your application returns HTML.
One possibility to solve this problem is PHP-GTK. PHP-GTK is a API for GTK (the graphical user interface used by Linux Ubuntu by default), written in PHP. Using this solution would have to read some documentation about this API, and you would need to rewrite some parts of your program, but it would probably be the most beautiful solution, because it would create a "native" experience.
Another possible solution could involve Adobe AIR, which lets your create programs for the desktop, using HTML, CSS and JavaScript, but I don't know if and how this would work together with your compiled PHP.
Please also note that it isn't absolutely necessary to compile your PHP for it to run on the client's computer; You could also ship the PHP-interperter right with your (uncompiled) PHP-script. Of course, compiling brings benefits, such as faster execution of the program.
-- André
I have a PHP class I want to convert to a PHP extension. I checked some tutorials (tuxradar's writing extensions, php.net's extending php, and zend's extension writing) and it's a bit complicated.
I found the article "How to write PHP extensions" (ed note: site is defunct) and I wanted to know if it is possible to use this to make it grab a PHP class from a certain path (say /home/website1/public_html/api/class.php), execute it and return the class instance.
This way it will be usable in other websites that are hosted on the same server – each can simply call the function and it will obtain its own instance.
Is that possible?
The question as I understand it now is, The user has a PHP class that they would like to share with multiple people, but does not want to share the source code.
There are many solutions to this, they generally invovle turning the PHP code into some kind of byte code, and using a PHP extension to run the byte code. I've never used any of these solutions, but I'm aware of the following:
phc is an open source compiler for PHP
Zend Guard
HipHop for PHP - I'm unsure about this, but Facebook recently released it so it might be worth a look.
I'm sure there are others. Just Google for PHP Compiler, or PHP Accelerator.
In one sentence: I don't believe so, I think its a lot more work than that.
No, there is not tool that can do that.
Anyway, what you want call be easily accomplished with auto_prepend_file. Just make that ini directive point to a PHP file that has the class definition, and then it will be available to all the applications.
If you don't want the users to be able to use the source, you can use one the several zend extensions that allow you to pre-compile the file and use it in that form.
You can extend underlying C library functions into PHP space by writing PHP extensions. However, i think in your case you don't need to write one.
I am aware that this is an old question (being from 2012) however the answer has changed and there is now a tool that can do this. Jim Thunderbirds PHP-to-C Extension toolset provides the means to take a simple class in one file all the way up to a complicated multi file multi-level namespaced framework and convert it to a C-extension that can then be installed into your PHP server.
While in many use cases doing so is not needed as the ordinary PHP code will work just as good in some cases significant performance improvements can be experienced. The information page shows that an ordinary class (deliberately designed to take a long time) took 16.802139997482 seconds as plain vanilla PHP, and 3.9628620147705 as a PHP extension built using the tool.
As an added advantage the tool also provides an additional feature. The ability to combine PHP code (to be converted to C) and native C code within the same extension which can produce even greater performance enhancements. The same example used above only tool 0.14397192001343 seconds when much of the intensive code was moved to a bubble sort C code and simply calling it from within the PHP code.
As a side note functionally to the end developers using the code using the extension is very much similar to having the files manually included in the PHP file being developed except it doesn't have to be specifically included as it is done through the PHP extensions component.
(Disclaimer: I am not affiliated with this developer but am glad to have come across it as it is thus far working for converting some of my intensive classes into PHP extensions without needing to know C).
I'm doing PHP development and I'm thinking of using one of these. I have both PHP and Java installed on my machine. In theory I could use any of the two.
What are the compelling arguments to pick Phing over Ant?
The greatest argument I have in favor of Phing is that it's fully developed in PHP ; which means, at least :
You can debug it / submit patches if you want
You can develop tasks in PHP
including tasks which are specific to your project
that can use stuff from your project (I've written a couple of tasks that use functions of Drupal, in a drupal-based project)
And, if you are working on a PHP project, your team probably knows PHP well -- while they might not know Java
No need to install Java on your servers
The main argument (for me) for using Phing when doing PHP development is
staying inside the PHP Ecosystem.
When using Phing on shared hosts, you can be sure it works when PHP works.
And you can easily extend Phing with your own task in PHP.
And since it's a PHP tool you will sometimes find it in other applications as well.
I'm trying to accomplish a task and turns out that the code I need is packaged as a PHP extension, which according to what I've been told means I have to have root access to install it (I'm on shared hosting so that's a bit of a problem.
I'll solve this problem later, but for now I'm trying to understand the difference between an extension, a library, and a class. Is it more of a packaging thing that could be overridden and repackaged a different way, or is there a valid architectural reasoning behind it?
Also when releasing your own code, what makes you decide to release as library vs. class vs. extension? or do you go with whichever sounds better?
thanks in advance.
P.S. If you must know which extension I'm talking about, it's Libpuzzle, but that's really beside the point, my question is more general.
An extension is a pice of code programmed in C which will be included into the PHP core when PHP starts. Normally you have some more native functions available after including a extension. For example a zip functionality.
A class is a abstract pice of PHP code which solves common tasks. For example sending emails. You can find some common classes at pear.php.net.
A library is a collection of PHP classes wich solve more generic tasks for example buliding HTML forms AND sending emails. The Zend Framework is a framework which consists of many, many PHP classes.
Normally extension features can be programmed in PHP. For example the PEAR::Compat class. Often you will find the functionality you need as a PHP class available. I'm sure the stackoverflow readers will supply you with ideas where to find a specific PHP class.
Extensions are low-level. Usually written in C/C++, and compiled into native-code shared libraries, they interact with the Zend Engine directly. It has pros and cons, main advantages being the speed and more control; and main disadvantages - they are harder to install, and require compilation (and that requires a compiler and PHP headers); it's not true they require root access though - you only need ability to use custom php.ini (or dl() function, but I see they deprecated it for some reason).
Libraries/classes are high-level and interpreted. If you don't know if you need to write extension, then you probably don't. About what classes are - read about OOP. A library is a reusable collection of code (most commonly in form of functions/classes).
Some libraries (including libpuzzle) also include a command-line tool. So if you're unable to use the PHP library due to your shared hosting environment, maybe you can compile the command-line tool. Then you can run it from PHP using something like exec. It will be slower and require more memory than a library, but it might get the job done. Of course, many hosts also have restrictions on commands like exec, so this might not work either.