I'm working on a custom CLI command & I was wondering what's the best way to call other commands from the PHP code (without shell_exec() or similar).
For example:
When running "php bin/magento my:custom:command", it'll do it's thing & in the end will run "php bin/magento cache:flush".
Any Ideas?
Thanks.
The Magento CLI is built on top of Symfony Console. You can load up and run other commands with this component as such:
$arguments = new ArrayInput(['command' => 'my:custom:command']);
$this->getApplication()->find('my:custom:command')->run($arguments, $output);
$arguments = new ArrayInput(['command' => 'cache:flush']);
$this->getApplication()->find('cache:flush')->run($arguments, $output);
More information here. Although it's unlikely to be a problem for you, please note that the documentation suggests this is not always the best idea:
Most of the times, calling a command from code that is not executed on the command line is not a good idea. The main reason is that the command's output is optimized for the console and not to be passed to other commands.
Related
I need to run this command when user change something in translation file
php artisan export:messages-flat
I need to add it in may controller
so I'm using this code
\Artisan::call('export:messages-flat');
but it return error saying that
The command "export:messages-flat" does not exist.
but when I
php artisan list
it's in the list
I also try to run other command
\Artisan::call('cache:clear');
and it works
this is the package I'm using link
kindly help me, sorry for may poor english
The package only allows you to run the commands from the CLI. You can see from the source code, they only register the Artisan commands if the application is running from the console.
As an alternative, you can call ExportLocalization::export()->toFlat() according to their documentation.
I have a problem figuring out how make my command work, because I only have access via ftp.
The action is pretty simple:
public function actionRun($action = "default") {
$this->xml = simplexml_load_file('db.xml');
return $this->{$action}(); // executes the default() method
}
All I need is to somehow execute the php index.php mycommand run, but I'm lost as to how. This command should only be run once in the life of the whole app.
My question is, is it possible to run such a command? Maybe somehow invoke it through php?
Yii command line command are designed to run through Yii.
$ cd protected
$ ./yiic --help
$ ./yiic mycommand
If you only have ftp access, you may be out of luck, and will have to use some workaround, for example running a cron job, or creating a web page that invokes the last command in the code sample I provided.
echo exec('/my_yii_dir/protected/yiic mycommand');
I'm using GREE Labs' Dbus PHP Extension in my attempts to make a PHP class that is able to create desktop notifications.
$dbus = $dbus = dbus_bus_get(DBUS_BUS_SESSION);
$message = new \DBusMessage(DBUS_MESSAGE_TYPE_SIGNAL);
$message->setDestination("org.freedesktop.DBus");
$message->setAutoStart(true);
$dbus->sendWithReplyAndBlock($message, 1);
When my code is run I get the following error:
Warning: dbus_bus_get() [function.dbus-bus-get]: failed to create dbus
connection object [Unable to autolaunch a dbus-daemon without a
$DISPLAY for X11] in [...COI/GTK/Notify.php on line 39
This is the first time I've used anything related to dbus, and am rather lost.
I'm aiming for an effect similar to what occurs when one executes the following in a terminal (on Ubuntu 11.10):
/usr/bin/notify-send -t 2000 'title' 'message'
I did initially use notify-send & exec, but switched when I found the GREE Dbus extension as I thought it may provide a cleaner interface. Also notify-send would only work properly if I changed my apache user to be the same as the user I'm currently logged in as - not an ideal solution.
Would anyone be able to either tell me what modifications are required to achieve my desired result, or alternatively tell me if what I want to do is in fact impossible?
Or, is there another way I should be doing this?
Dbus does not like being run while in a command line environment, without X. It's frustrating, but this is what I wrote in python to override that. It comes down to setting two environmental variables.
def run(self):
os.environ['DBUS_SESSION_BUS_ADDRESS'] = "unix:path=/run/dbus/system_bus_socket"
os.environ["DISPLAY"] = ":0"
try:
bus_name = dbus.service.BusName(INTERFACE,
bus = dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name,
'/com/your/path/here')
gobject.MainLoop().run()
except Exception, E:
f = file('/tmp/bus.log', 'a')
f.write(str(E))
f.close()
EDIT: I forgot another very useful way to run dbus on the command line
eval 'dbus-launch --auto-syntax' [command]
I use it on a raspberry pi to run my custom dbus deamons. dbus-launch --auto-syntax is a command that outputs environmental variables and files applicable to dbus in bash. The eval command will take that output and evaluate it so your command will run with those environmental variables.
A simple test would be to run something like this:
eval 'dbus-launch --auto-syntax' python /usr/bin/my-dbus-daemon.py
eval 'dbus-launch --auto-syntax' qdbus org.dbus.method /org/dbus/method/test
Use dbus-launch in the script that starts your web server in order to start an appropriate DBus daemon at the same time. See the dbus-launch(1) man page for details.
Is this possible?
What I want to do is send:
run_app.exe -param 'test' -name 'tester'
to a windows cmd line from PHP.
Is this possible or do I need to write a windows service that is somehow triggered by the application?
You can use exec() for that.
Have you tried exec?
Or you can use:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run(strCommand, [intWindowStyle], [bWaitOnReturn]);
Here you can find the run method params: http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.85%29.aspx
And here is the COM class doc: http://www.php.net/manual/en/class.com.php
With this method you can do so much more in windows :).
I used it becaus of the [bWaitOnReturn] parameter which I couldn't do using any other method.
Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');
$strCmd1 = 'run_app.exe -param "test" -name "tester"';
$return1 = $shellObj->exeCmd($strCmd1);
The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.
I wrote a script to parse some data from a website using cURL and it works fine when I run it in my browser, however when I want to run it in the command line I get the error "call to undefined function curl_init()". Do php scripts run under different settings from the command line?
This is happening because you are simply trying to call a PHP function from bash. If you have curl installed in your linux environment then the command should simply be curl [-options] [url]. The simplest of them being something like:
$ curl http://someurl.com/path/to/xmlfile.xml
You can test for this from the command line by tying "$ which curl" (without the quotes of course). That will give you the path to where it is stored in case you have to use the full path. (e.g. /usr/bin/curl [-options] [url]).
EDIT:
after having re-read your question I realized that I dumbly missed the fact that you said you were trying to run the PHP script from the command line and not curl itself. And now I, too, am stumped by your problem. Sorry!