Symfony console, file paths, /dev/fd/0 trick - php

I have my own symfony console commands, some of which take a filename as the argument.
eg.
$ php app/console mycommands:doTheThing /path/to/my/file.txt
I have tried using the /dev/fd/0 trick to use a string instead of a file here, however it's still not working.
eg.
$ php app/console mycommands:doTheThing php://dev/fd/0 <<<'myFileContents'
Could anybody provide any insight or help?

You can use process substitution:
$ php app/console mycommands:doTheThing <(echo "foobar")

Related

/usr/bin/less: unrecognized option: X laravel docker error

i am using laravel on docker . i run my project when i exec into container and use
php artisan tinker
no matter what command i run i receive this error :
bash-5.1$ php artisan tinker
Psy Shell v0.11.8 (PHP 8.0.14 — cli) by Justin Hileman
>>> App\Models\User::where('id',12)->first()->createToken('testToken');
/usr/bin/less: unrecognized option: X
BusyBox v1.34.1 (2021-11-23 00:57:35 UTC) multi-call binary.
Usage: less [-EFIMmNSRh~] [FILE]...
View FILE (or stdin) one screenful at a time
-E Quit once the end of a file is reached
-F Quit if entire file fits on first screen
-I Ignore case in all searches
-M,-m Display status line with line numbers
and percentage through the file
-N Prefix line number to each line
-S Truncate long lines
-R Remove color escape codes in input
-~ Suppress ~s displayed past EOF
RuntimeException with message 'Error closing output stream'
any idea what can be wrong here ?
Busybox contains cutdown versions of many Unix/Linux utilities, including less. Either remove it and install the less package, as well as the other utilities it mimics, or hack artisan and remove the -X switch against /usr/bin/less. All the -X switch does is sort alphabetically by entry extension
Ugh just spent a ton of time messing with this one myself.
The root cause of this issue is the upgrading of the psypsh shell package that tinker users.
You can see in the release notes here https://github.com/bobthecow/psysh/releases/tag/v0.11.3
There are a few ways around this, as mentioned above, you can just install the less package using apk or apt-get.
You can also set the cli.pager property in your php.ini file to explicitly call less without the -X switch.
More info here: https://github.com/bobthecow/psysh/issues/717

Why is PATH on mac not working? [duplicate]

Can anyone explain how python 2.6 could be getting run by default on my machine? It looks like python points to 2.7, so it seems like which isn't giving me correct information.
~> python --version
Python 2.6.5
~> which python
/opt/local/bin/python
~> /opt/local/bin/python --version
Python 2.7.2
~> ls -l /opt/local/bin/python
lrwxr-xr-x 1 root admin 24 12 Oct 16:02 /opt/local/bin/python -> /opt/local/bin/python2.7
When I generate an error, I see what's really getting run. Why could this be?
~> python -error-making-argument
Unknown option: -e
usage: /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
And how can I correct it?
From suggestions in comments:
~> alias
alias cp='cp -i'
alias gcc='gcc -Wall'
~> type python
python is /opt/local/bin/python
Bash uses an internal hash table to optimize $PATH lookups. When you install a new program with the same name as an existing program (python in this case) earlier in your $PATH, Bash doesn't know about it and continues to use the old one. The which executable does a full $PATH search and prints out the intended result.
To fix this, run the command hash -d python. This will delete python from Bash's hash table and force it to do a full $PATH search the next time you invoke it. Alternatively, you can also run hash -r to clear out the hash table entirely.
The type builtin will tell you how a given command will be interpreted. If it says that a command is hashed, that means that Bash is going to skip the $PATH search for the executable.
I just checked my .bash_profile, and it contained the following:
# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:/usr/local/git/bin:${PATH}"
export PATH
Commenting this out has fixed my problem.
If someone can tell me why which and type still gave incorrect answers, I'd be very grateful, and will give them a check-mark!
Thanks for all your guidance!

Why is $this->argument() Showing the Name and the Value?

In Laravel, I am making a console command. Per the docs, you should be able to get the value of the argument using $this->argument():
$userId = $this->argument('user');
I have an argument that is an integer 1. However, $this->argument('some_name') is returning a string such as some_name=1, instead of simply 1
Is there a setting or something that I missed?
Arguments don't get named, unlike options. For example:
$ php artisan command argument1 argument2 argument3
$ php artisan command --option1=foo --option2=bar
So, I'd either change the definition of your argument to an option so that you can run:
$ php artisan command --some_name=1
Or, you can keep using this as an argument and run:
$ php artisan command 1
Note: artisan and command in the above examples are arguments of the php executable.

php CLI mode -f flag

I was reading about specifics of PHP in CLI mode and I can't explain for myself what utility has -f flag.
It's possible execute any php script as "php name_of_script.php" or "php -f name_of_script.php"
I guess this option is just kind of syntactic sugar. Also its existence can be perhaps explained by the fact that it's more obvious for user when he sees -f that file is executed. I can't make up any other explanations. Do someone see any other usage of it?
PHP has a very long history with a lot of the design decisions lost in the mists of time; I'm not sure anyone will be able to tell you for certain why there's both a -f option and the ability to run a file without any options at all.
However, it certainly seems designed for user convenience; most command line users would expect an interpreter to interpret a filename provided as a single parameter, and it's the most common use-case, so making it the quickest to type makes sense. My guess would be that the PHP CLI started off with just the -f option, and the option to run a file by providing just the filename was added later to make people's lives easier. The -f was retained for backwards compatibility.
I can think of one case where the -f option is useful: if the filename starts with a hyphen, for example -.php.
When provided as a single parameter, this will be treated as an option, and fail:
$ php -.php
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
php [options] [-B <begin_code>] -R <code
...
However, with -f, it'll work:
$ php -f -.php
<script executes successfully>

PHPUnit setup in Batch File

I am not preferring using PEAR method, so I decided to use another way, However, I still cannot make a perfect .bat file to run in two situations.
I downloaded the phpunit.phar and the path I had added into PATH => C:\dev
e.g C:\dev\phpunit.phar
I also create a phpunit.bat file inside C:\dev\phpunit.bat, this allow me to run phpunit in any place.
This is the content of the phpunit.bat
#echo off
php c:\dev\phpunit.phar
I have run perfectly result when I am using the guard-phpunit. However, the problem is when I want to pass an argument / parameter to the phpunit it does not work.
E.g I got a testing file in C:\testing\CalculatorTest.php
When I run
C:\testing>phpunit C:\testing\CalculatorTest.php
or
C:\testing>phpunit CalculatorTest.php
It only output like php phpunit.phar, it does not take the argument.
It only work when I enter the command like this
C:\testing>php c:\dev\phpunit.phar CalculatorTest.php
I try to edit the phpunit.bat file to make it like below:
#echo off
set arg1=%1
:start
if "%1"=="" (goto :main)
REM without any argument
php C:\dev\phpunit.phar
goto :end
:main
php C:\dev\phpunit.phar %arg1%
:end
The above code will work in C:\testing>php c:\dev\phpunit.phar CalculatorTest.php, but when run in guard-phpunit it will keep prompting option --include-path requires an argument error.
Anyone know how to fix the .bat so it able to run in both situation?
Thank you so much!
I have created phpunit.bat file in same directory of the phpunit are instaled, for example B:\libs, containing:
#ECHO OFF
php "%~dp0phpunit.phar" %*
and in the windows PATH variable I added ;B:\libs:
Hold Win and press Pause.
Click Advanced System Settings.
Click Environment Variables.
Append ;B:\libs to the Path variable.
Restart Command Prompt.
After restart the terminal PHPunit works:
PS C:\Users\joridos> phpunit.bat --version
PHPUnit 4.1.3 by Sebastian Bergmann.
Use for phpunit.bat:
#echo off
php c:\dev\phpunit.phar %*
%* is replaced on execution by whatever was passed to phpunit.bat which means nothing if nothing was passed to phpunit.bat, or first + second + third + ... arguments exactly as specified on calling phpunit.bat.

Categories