4 useful tips when creating PHP console tools

table of contents
Hello.
I'm Mandai, in charge of Wild on the development team.
We often feel the urge to create a simple tool to automate tedious tasks. It's what
people call a "spur of the moment" kind of thing, but as you've probably experienced, such tools tend to have a specific purpose, so they don't require you to open a browser to run them. That's
because we want to automate the process of opening a browser.
In such cases, I would use a console tool and run it periodically, but since PHP can also run console applications, I have collected some useful tips for such situations.
Please note that I have only tested it on Linux.
Get the home directory path
Speaking of consoles, shell scripts are used, and in the
case of shell scripts, the home directory path can be obtained using "~".
If you try to load a file in the user's home directory using a similar path in PHP, an error will occur
<?php echo file_get_contents('~/hogehoge.txt'); # 上記のソースを実行すると、以下のエラーが発生 PHP Warning: file_get_contents(~/hogehoge.txt): failed to open stream: No such file or directory in Command line code on line 1
So, you might want to get the executing user, add /home, and do various other things to create a path, but
the home directory path is properly stored in the super global variable $_SERVER.
<?php echo file_get_contents($_SERVER['HOME']. '/hogehoge.txt'); # 上記のソースを実行すると、テキストファイルの中身が表示される hogehoge
It's handy when you want to put a configuration file in the user's home directory, similar to bash.
It's a blind spot (was) that data is stored in the $_SERVER variable even though it's executed from the console.
It's a hidden parameter that isn't even mentioned in
the documentation (It's mentioned briefly in the comments after the main text.)
How to take arguments
Changing behavior depending on arguments is a natural thing in the command line world, so it can of course be done in PHPCLI too.
However, even though it is a super global variable,
- Does not start with _
- Not defined in uppercase
- Is it c? Is it v? Please be clear!
As a result, I can't remember the words right away, and if I don't use them for a while, I forget them
The superglobal variable that stores the arguments passed at runtime $argv .
Also, if you want to pass a string with line breaks to $argv, enclosing the string in "" or ' will make it work as intended
For example, prepare a source that simply var_dumps the following arguments directly
<?php var_dump($argv);
Next, let's run the source code with a string containing line breaks as an argument
php test.php hogehoge 'I'll add a newline' # When you run the above command, the following will be displayed: array(3) { [0] => string(8) "test.php" [1] => string(8) "hogehoge" [2] => string(22) "I'll add a newline" }
It looks like a lot of things will be progressing
Adding color to error messages (+bonus)
If you want to have some fun, why not try coloring your error messages?
In Linux, you can make the echoed string colorful and change the background color
This can easily be achieved using the same technique
<?php # hogehogeを赤字で表示するコード echo "\e[31mhogehoge\e[m"; echo "\033[31mhogehoge\033[m";
Both of the above codes will display hogehoge in red, but at first glance it may not make sense, so
let's break it down a bit.
<?php # hogehogeを赤字で表示するコード echo "\e[31m". "hogehoge". "\e[m"; echo "\033[31m". "hogehoge". "\033[m";
Let's separate the "hogehoge" part from the rest. Then, we can see a pattern. It seems to be
a good idea to enclose the string you want to change the color of in " \e[**m " or " \033[**m ".
Next, there is the "31" that is only attached to the beginning, and
this is the part that specifies the text color.
31 is divided into 3 and 1, with 3 specifying the text color and 1 specifying red.
In other words, if you want to specify a different text color, change 1 to 2.
I wrote that the 3 in 31 specifies the text color, but if it becomes 4, it specifies the background color
.
<?php echo "\e[41m". "hogehoge". "\e[m"; echo "\033[41m". "hogehoge". "\033[m";
The above code will result in a red background
If you want to change both the text color and the background color, place them side by side with a semicolon
<?php echo "\e[31;41m". "hogehoge". "\e[m"; echo "\033[31;41m". "hogehoge". "\033[m";
The code above will just display a red rectangle and no text, but it will achieve its purpose
As for more advanced features, you can also use this setting to underline, bold, invert the text and background colors, and make the text blink
<?php echo "\e[31;1m". "hogehoge". "\e[m"; // 赤い太字で表示 echo "\e[31;42;1m". "hogehoge". "\e[m"; // 赤い太字を緑の背景で表示 echo "\e[7m". "hogehoge". "\e[m"; // 文字色と背景色を反転して表示
Inversion and other features are less likely to strain your eyes, so you can use them alone
Command options to use when you want to quickly check only specific code
When creating a console tool, you
may suddenly have small questions, such as what form the execution results will be returned in, or whether writing it this way will result in an error. In this article,
we will introduce the PHP option "-r" that you can quickly try in such cases.
For example, let's say you want to change permissions.
You can quickly change it with the chmod function, but if you want to test it, you don't have to bother writing test code. You can write PHP code directly in the command line
.
php -r "chmod('/path/to/hoge', 777)" # You can see the results immediately with the ls command!
I intended to write it like chmod 777 /path/to/hoge, but the permissions were not actually changed.
I read the documentation and tried and failed to find the answer.
Edit the file → Save → Go to console → Run the php file → (´・ω・`)
To be honest, repeating this process over and over is a pain, so if you can extract something and test it easily like in the example above, it's probably less work to do it from the command line
That's it.
0