Naguel

I'm Nahuel, and these are my work experiences, ideas and thoughts as a web developer working on the eCommerce industry.

Configure the Terminal to use MAMP's PHP

Configure the Terminal to use MAMP's PHP

By default, after installation, MAMP will make its PHP binaries "available on the browser" while the Terminal will keep on using the system's PHP with its own configuration.

The idea behind changing the Command-line to start using the PHP coming with MAMP and its configuration is to be able to switch rapidly between PHP versions and to have the configuration for PHP in only one place.

Out there you can find enough guides that helps you archive this because, frankly, there are plenty of methods to get this done. This next one is how I personally do it because it's easy to implement and it also covers something most of other guides won't which is configure the Terminal to also use the same php.ini MAMP uses.

Configuring the Command-line to use MAMP's PHP

You need to edit your Terminal's Profile in order to add the following to the end:

#export PATH=/Applications/MAMP/bin/php/php7.1.33/bin:$PATH
#export PATH=/Applications/MAMP/bin/php/php7.2.33/bin:$PATH
export PATH=/Applications/MAMP/bin/php/php7.3.21/bin:$PATH
#export PATH=/Applications/MAMP/bin/php/php7.4.9/bin:$PATH

As you can see I'm adding a different line per PHP version I want to potentially have available on the Command-line (PHP 7.1.33, PHP 7.2.33, etcetera) but having them all but one (PHP 7.3.21) commented with the # at the beginning.

Every time you switch the PHP version in MAMP you should come back to the Profile and leave uncomment the same version so the Terminal and MAMP match.

Your Command-line's Profile file depends on your shell. If you are using the default Terminal coming with macOS chances are the Profile will either be ~/.bash_profile or ~/.bashrc. Mines it's ~/.zshrc because I use Oh My Zsh.

Remember that everytime you change your Profile you need to "reload" it by doing source ~/.bash_profile (or whatever file you are using).

You can check if everything was applied as expected by executing php --ini and seeing the paths are pointing to MAMP.

Configuring the Command-line to use MAMP's php.ini

Here's something interesting about MAMP Pro: it generates, each time it starts, the final php.ini file it will be using during the execution as its content depends on the settings configured on the software’s UI.

For example, if you enable/disable Xdebug on MAMP by ticking/unticking the checkbox on the app, MAMP will regenerate the php.ini file with your configuration (this is basically how MAMP applies any setting change that you perform from the UI).

The final generated php.ini file is located at /Library/Application Support/appsolute/MAMP PRO/conf/php.ini.

We already configured the Terminal to use MAMP's PHP binaries but we also need to configure it to use the generated php.ini by going to /Applications/MAMP/bin/php/php7.3.21/conf (where the not auto generate php.ini file is located) and delete it (after a back up).

Then we need to create a symlink called php.ini for /Library/Application Support/appsolute/MAMP PRO/conf/php.ini (which is the auto generated one by MAMP) by doing:

ln -sf /Library/Application\ Support/appsolute/MAMP\ PRO/conf/php.ini php.ini

The conf folder should ended up looking like this:

Of course the example is for PHP 7.3.21 but you will need to repeat this for the folder of each PHP version you will be using on the Command-line.

This only applies to MAMP Pro as the non-Pro version doesn't generates any php.ini and if you want to change something you need to edit the original file yourself.

Discover on what commit a bug was introduced using git bisect

Discover on what commit a bug was introduced using git bisect

The git bisect command allows us to performs a binary search to find out what commit causes a bug. It's the fast version of going through each commit from the first one to the last one until you finds out that "Oh, after this commit the bug can be reproduced".

Basically what you do is tell Git where things were working (meaning on what commit you remember the bug didn't appear), and then you tell Git where things are not working (meaning a commit where the bug can be reproduced, that can be the last one).

It's easy with an example

In theory sounds great but it's better if we go through an example to learn how to use the git bisect command.

I have a small fake project with a series of commits and I introduced a bug in one of them. Of course, the commit messages are self explanatory but we need that to see if the git bisect really works.

We are going to find out on what commit a typo was introduced on the content of my example.txt file.

Finding the commits where things works and doesn't

This is the easy part and you probably know how to do it.

Checkout to an old commit, see if the bug disappeared, write down the commit hash, and come back to the HEAD. And you can use the newest commit hash as the bad point.

Here's the list of my commits and the one I choose for the good and bad points.

commit 9cc1e21a4422a3e1365bd04b616ddafd98475c3e
Improved ways to contact us.
(here things are bad)

commit 785361159507faa755dfb4a1aaaac8cb7ad18c7e
Added more text to encourage people.

commit 5603f08930ad4aaf84843f6205f25cdd0f684f4f
Added a signature.

commit 45493f69eba7153d7d954f0eeb7e4973e32d9f3a
Added a way to contact us in case he/she finds a typo.

commit 9487c88848e056f120166e0a5844822cb0f6b8a4
Added even more text to the example.txt file to encourage the user to report any typo.

commit c04e336490796f22423b9685c0ffe745ebcddcbb
A wild typo appears...

commit 2fec0d56c80fd1f7f000ab31ecba643d4c12533d
Improved example.txt file by adding more text to it.

commit 979ba26a9e0eaeae35e0e236bc14126708a942a6
Updated example.txt file with some text without a typo.
(here things are good)

commit 27c9d5ec2bcae6f1aa8ee295483ac9696e579296
Initial commit with example.txt empty.

Indicating the commits where things works and doesn't

You have the good and the bad, let's transfer that knowledge to Git.

git bisect start
git bisect good 979ba26a9e0eaeae35e0e236bc14126708a942a6
git bisect bad 9cc1e21a4422a3e1365bd04b616ddafd98475c3e

Bisecting

Git will move you to any of the commits trap in the middle of the good and bad points. And the only thing you need to do is to tell Git if that commit is a good or a bad one, meaning if you can reproduce the bug or not.

Here's what I get:

➜  Bisect_Project git:(master) git bisect start
➜  Bisect_Project git:(master) git bisect good 979ba26a9e0eaeae35e0e236bc14126708a942a6
➜  Bisect_Project git:(master) git bisect bad 9cc1e21a4422a3e1365bd04b616ddafd98475c3e
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[9487c88848e056f120166e0a5844822cb0f6b8a4] Added even more text to the example.txt file to encourage the user to report any typo.
➜  Bisect_Project git:(9487c88)

I check the example.txt file to see if the typo is there or not.

This test hat no typo. Please, feel free to check it.

If you find a problem, please report it.

Is there (it says hat instead of has), so it's a bad commit. I tell Git about it and he move me to anothe commit.

➜  Bisect_Project git:(9487c88) git bisect bad
Bisecting: 0 revisions left to test after this (roughly 1 step)
[c04e336490796f22423b9685c0ffe745ebcddcbb] A wild typo appears...

I check the example.txt file again.

This test hat no typo. Please, feel free to check it.

The typo is there, I tell Git about it and he'll move me again.

➜  Bisect_Project git:(c04e336) git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[2fec0d56c80fd1f7f000ab31ecba643d4c12533d] Improved example.txt file by adding more text to it.

I check the example.txt file one more time.

This test has no typo. Please, feel free to check it.

No typo! It's a good commit.

➜  Bisect_Project git:(2fec0d5) git bisect good
c04e336490796f22423b9685c0ffe745ebcddcbb is the first bad commit
commit c04e336490796f22423b9685c0ffe745ebcddcbb
Author: nahuelsanchez <nsanchez@summasolutions.net>
Date:   Fri Feb 19 22:52:04 2016 -0300

    A wild typo appears...

:100644 100644 5aebed1a496b67a06fe93b31d9590eae6fb258ad 657a182a496be92d26c948ece9cd9fa0061e03bb M	example.txt
➜  Bisect_Project git:(2fec0d5)

And there we have it. Git is telling us on what commit the bug appeared for the first time. It's up to your skills to check the code and fix it.

When you want to return to the HEAD just do a git bisect reset and you'll be back.

Install Composer on OS X and use it globally

Install Composer on OS X and use it globally

Magento 2 needs Composer, so let install it on OS X in a way it can be used globally.

Considering that installing Composer will create a composer.phar file, and our intention is to use it everywhere, anytime, regardless the project we're working on, I decided to install it (meaning let the composer.phar be created) in /Applications/Composer (it makes sense to me, but you can choose the destination folder you want).

Installing Composer

First thing first, let's go to /Applications, create the Composer folder, and get in.

cd /Applications/
mkdir Composer
cd Composer

Now, we can install Composer for real using cURL.

curl -sS https://getcomposer.org/installer | php

If everything goes as expected we'll get a nice message saying something like "Composer successfully installed to..." and we can proceed to the next step.

Making it globally available

Now that we have the composer.phar file, we need (or want?) to have it globally available by typing composer in the command line. To achieve this let's start by moving our composer.phar file to /usr/bin/.

sudo mv composer.phar /usr/bin/

And finally let's create an alias on our bash profile.

alias composer="php /usr/bin/composer.phar"

Reload the command line profile, and that's all it takes to get Composer on OS X. You can now go to any folder you want and type composer about. If something nice appears it means everything is okay.

I might be skipping the part where I'm supposed to explain how to edit and reload the bash profile. It was on purpose because it always different depending on the terminal you use (for example mines it's located in ~/.zshrc because I use Oh My ZSH!).

You can try the following in order to access and edit the bash profile.

vim ~/.bash_profile