Skip to content

Instantly share code, notes, and snippets.

@richardjortega
Last active April 24, 2016 23:59
Show Gist options
  • Save richardjortega/9623840 to your computer and use it in GitHub Desktop.
Save richardjortega/9623840 to your computer and use it in GitHub Desktop.
Psysh Install CodeUp
<?php
echo "Enter the amount in US dollars: $";
$money = read_stdin();
echo "What currency do you want to convert to? (E)uros, (P)esos, (B)ritish Pounds: ";
$currency = strtoupper(read_stdin());
$converted = convert($money, $currency);
echo "$$money is equivalent to $converted euros\n";
function convert($money, $currency)
{
$conversion_dict = array(
"E" => 0.73,
"P" => 13,
"B" => 0.60
);
## Old-school debugging
# echo $conversion_dict; --> this won't work at all
# var_dump($conversion_dict);
# print_r($conversion_dict)
## New school debugging
# Type `debugger` then hit tab (after creating the snippet)
$converted_value = $money * $conversion_dict[$currency];
return $converted_value;
}
// our function to read from the command line
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
?>

PsySH Install [for CodeUp]

Add PsySH to Composer Globally

$ composer global require psy/psysh

If asked what version constraint to use just type *

If asked to type in your GitHub credentials please do as Composer grabs from the Github repo

Add PsySH to CLI

Update your PATH for global CLI use

Add the following to the .bashrc file

$ vi ~/.bashrc
  • Hit Shift+g [takes you to bottom of page]
  • Hit o [opens new line]
  • Hit Enter [add new line]
  • Add the following line: export PATH=~/.composer/vendor/bin:$PATH
  • After modifying the file hit Esc and type :wq then enter
  • Exit SSH from Vagrant box with exit
  • Now log back into your vagrant box with $ vagrant ssh

PsySH in the CLI

$ psysh

Use Debugger in your PHP

require('/Users/richardjortega/.composer/vendor/autoload.php'); 
# find path with `which psysh`
\Psy\Shell::debug(get_defined_vars());
# code stops line before with interactive shell!!!

Let's create a Sublime Text snippet

  • Load up Sublime Text
  • Tools > New Snippet

Copy and paste the following code:

<snippet>
  <content><![CDATA[
require('/home/vagrant/.composer/vendor/autoload.php');
\Psy\Shell::debug(get_defined_vars());
]]></content>
  <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  <tabTrigger>debugger</tabTrigger>
  <!-- Optional: Set a scope to limit where the snippet will trigger -->
  <!-- <scope>source.python</scope> -->
</snippet>
  • CMD + S, Sublime Text 2 or 3 > Packages > User [Sublime will open the correct folder to save for you] and save as psysh_debugger.sublime-snippet
  • Now you can just type debugger then hit TAB and it will add the necessary lines for your breakpoint.
  • You can add multiple breakpoints in your page.
  • To remove a breakpoint simply remove/delete the two inserted lines.

Let's play around

@ryanorsinger
Copy link

Thanks for sharing this with our CodeUp cohort today, Richard! Sharing new tools and enthusiasm is much obliged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment