? No Code Attached Yet
avatar nikosdion
nikosdion
3 Apr 2023

Problem identified

Right now, running the Joomla CLI Application requires typing something like php /path/to/site/cli/joomla.php blah:blah.

Most PHP CLI applications —especially Symfony-based ones— can be called directly, e.g. /path/to/site/cli/joomla.php blah:blah

This constantly gets me. I imagine it's just as annoying for other console power users.

Proposed solution

Add the shebang at the top of the file, i.e. the first line of the file should be
#!/usr/bin/env php
Also make the file executable, i.e. chmod +x cli/joomla.php

Open questions

Installing by a ZIP file does not carry over the UNIX permissions. We might want to tell users to chmod +x cli/joomla.php somewhere in the documentation?

On Windows the shebang does nothing outside the WSL2. However, it also does NOT produce output — the PHP CLI binary strips it silently. I know this for a fact, as demonstrated by Composer, countless PHP CLI applications, and my own PHP CLI scripts and tools.

avatar nikosdion nikosdion - open - 3 Apr 2023
avatar joomla-cms-bot joomla-cms-bot - change - 3 Apr 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 3 Apr 2023
avatar richard67 richard67 - change - 4 Apr 2023
Labels Added: ?
avatar richard67 richard67 - labeled - 4 Apr 2023
avatar nyni123
nyni123 - comment - 9 Apr 2023

Hi @nikosdion ,

Thank you for bringing up this issue and proposing a solution to make the Joomla CLI Application more user-friendly. Before I start working on this, I would like to clarify some details with you.

  1. Have you already tried adding the shebang and testing if it works as expected?
  2. Are you planning to submit a pull request with the changes or are you looking for someone else to implement the proposed solution?
  3. Are there any other potential issues that you foresee with the proposed solution?

Your answers will help me better understand the issue and how I can proceed with implementing the proposed solution.

Thank you in advance for your response!

avatar nikosdion
nikosdion - comment - 9 Apr 2023

Hi @nyni123

  1. Yes, I did. Please note that it's two changes: a. add the shebang and b. make the file executable:
$ ls -l cli/joomla.php 
-rwxr-xr-x  1 nicholas  staff  3064 Apr  9 15:58 cli/joomla.php
$ head -n 1 cli/joomla.php
#!/usr/bin/env php
$ cli/joomla.php -V

Joomla! 4.3.0-rc3-dev (debug: Yes)
  1. I'll do it myself in a few minutes.

  2. Nope, there are no adverse effects. This is also what most PHP CLI tools do:

# WP-CLI
$ head -n 1 `which wp`
#!/usr/bin/env php
$ ls -l `which wp`
lrwxr-xr-x  1 nicholas  admin  29 Feb 25 09:09 /opt/homebrew/bin/wp -> ../Cellar/wp-cli/2.7.1/bin/wp

# Composer
$ head -n 1 `which composer`
#!/usr/bin/env php
$ ls -l `which composer`
lrwxr-xr-x  1 nicholas  admin  37 Feb 25 09:09 /opt/homebrew/bin/composer -> ../Cellar/composer/2.5.4/bin/composer

# PHPCS
$ head -n1 `which phpcs`
#!/usr/bin/env php
$ ls -l `which phpcs`
-rwxr-xr-x  1 nicholas  staff  3366 Aug  8  2022 /Users/nicholas/.composer/vendor/bin/phpcs

# Rector
$ head -n1 `which rector`
#!/usr/bin/env php
$ ls -l `which rector`
-rwxr-xr-x  1 nicholas  staff  3333 Aug  8  2022 /Users/nicholas/.composer/vendor/bin/rector

# PHING
$ head -n1 `which phing`
#!/usr/bin/env php
$ ls -l `which phing`
-rwxr-xr-x  1 nicholas  staff  3324 Jul 25  2022 /Users/nicholas/.composer/vendor/bin/phing

# PHPStan
$ head -n1 `which phpstan`
#!/usr/bin/env php
$ ls -l `which phpstan`
-rwxr-xr-x  1 nicholas  staff  3330 Dec 30 12:10 /Users/nicholas/.composer/vendor/bin/phpstan

# (My own) Akeeba UNiTE
$ head -n1 `which unite`
#!/usr/bin/env php
$ ls -l `which unite`
-rwxr-xr-x  1 root  wheel  1149632 Apr  9 16:05 /usr/local/bin/unite

On any system which has a compatible PHP version in the $PATH you're able to execute these CLI applications by just calling them as any other executable. This has been tested by yours truly on macOS, Ubuntu Server 20.04, Tuxedo OS 2 (a Debian-based desktop Linux distro), and Windows 11 WSL2.

On any system which does not understand shebangs (e.g. Windows 11 using PowerShell or CMD) it just doesn't execute unless you put the PHP-CLI binary in front of the file path, e.g.

c:\php\php81\php-cli.exe cli/joomla.php -V

Regardless of whether you use the above command line or associate .php files with the PHP CLI binary, the PHP CLI executable on Windows does not print out the shebang, as explicitly documented:

The special shebang first line for Unix does no harm on Windows (as it's formatted as a PHP comment), so cross platform programs can be written by including it.

I can confirm this is the case. I have been using Composer, Phing and various other PHP CLI tools —both third party and my own— on all three major platforms (Windows, macOS, Linux) for years and they all had the shebang.

If you have any other questions I will be glad to answer them, albeit a bit slowly as I'm on Easter vacation :)

avatar nikosdion
nikosdion - comment - 9 Apr 2023

Added Pull Request as instructed: #40351

avatar richard67
richard67 - comment - 9 Apr 2023

Closing as having a pull request.

avatar richard67 richard67 - change - 9 Apr 2023
Status New Closed
Closed_Date 0000-00-00 00:00:00 2023-04-09 20:33:31
Closed_By richard67
avatar richard67 richard67 - close - 9 Apr 2023

Add a Comment

Login with GitHub to post a comment