Interactive Plugin For CakePHP Console and DebugKit

Posted by Matt on Tue, Jun 09 2009

I teased this on twitter last week, so here's the official release. The interactive plugin is a super easy way to run code against your Cake app. You can use it either through a custom panel in the DebugKit or from the command line as a shell.

Download

Get it at GitHub

Instructions

I'll skip the install instructions, since they're included in the README. It's pretty easy to install. If you're using the latest stable release of the DebugKit you do need to apply a patch for better plugin handling. That fix is already in the trunk, so it's not a dirty hack.

What It Can Do

Let's get right to the good stuff.

Run Simple PHP

Pretty much any simple PHP commands will run:

10 % 4 //returns 2
is_array(49) //returns false

Run SQL

SELECT id FROM users WHERE email = 'test@test.com' //returns the id of the user
UPDATE posts SET published = false; //returns "No results found.", but does the update.

Use Cake Functions

__('Test Message', true) //returns the translated text matching the msg id for your current language.
Configure::write('Config.language', 'es');
__('Test Message', true); //returns the translated text in Spanish.

Use Cake Libs

Security::hash('my_pAssw0rd', null, true) //returns the hashed password
Set::extract('/id', array(array('id' => 3), array('name' => 'test', 'id' => 4))) //array(3, 4)

Use Cake Helpers

$html->link('Posts', array('controller' => 'posts')) //the html code for the link and the link itself
$form->input('Post.title'); //the html code for the field and the field itself

Use Your App Code

Post::find('first') //The first record from your posts table
User::findById(3) //The user record for id 3

Debugging

If you have a command that isn't returning the results you expect, you can turn on debug first:

Configure::write('debug', 2);
User::findByIdd(3) //will show error output

Notes

When using through the DebugKit you can stack commands, just separate them with a ";". However you can't define a variable in one command then use it in another. For example this doesn't work:

$i = 10;
$i ++;
echo $i;

You'll also notice I never explicitly declared any of the models I was using. The plugin does this automatically. You can write your model calls statically or just pretend you have an instance. These are all the same and will work:

Post::find('first');
$Post->find('first');
Post->find('first');
$this->Post->find('first');

The DebugKit = Awesomeness

Like most of you, I read this post from Mark Story on the new JavaScript for the DebugKit. Just reading that post, you don't get a full appreciation for how much work he (and others) have put into it. The JavaScript lib is virtually it's own mini-framework. Everything I needed to create this was already included - Ajax, Event handing, Elements...Really great stuff.

That's About It

Let me know if you hit any commands you want to run that aren't working. I tried to cover most of the basics.

Posted in Code

19 Comments

Erik Johnson said on Jun 09, 2009
Hey Matt,

Great plugin.. I've wanted to have something like this for a while to test cake calls (without debug()-ing everything within views). I haven't had a chance to test the debug_kit integration, but in terms of the console plugin, it works great. I wish there was a way to cycle through past commands (e.g. up/down shows previous lines); though this hasn't been an ability for me on any cake console scripts available. Would be a nice addition, if possible in later revisions though.

Keep up the good work.
Matt said on Jun 09, 2009
This actually works on Windows (Vista FTW!). I was hoping it was something the Cake shell was quietly doing behind the scenes, but I checked it on linux and it doesn't work there. So for now you can work around by switching to Vista :)
Erik Johnson said on Jun 09, 2009
... or not. I'd rather chew off my leg then go back developing on vista! lol.

Also, with your patch is it your intention to display "Interactive.interactive" as the menu item under DebugKit, or do you want it to show as "Interactive"?

if (strpos($className, '.') !== false) {
list($plugin, $className) = explode('.', $className);
}

It seems if you put use $panel in place of $plugin, the key is adjusted accordingly.
Matt said on Jun 09, 2009
The trunk DebugKit has support for panels setting their own title. If you were on trunk it would just be "interactive".
Javier said on Jun 10, 2009
My first test has been awesome!

Well, actually, it hasn't :P. I tried (with DebugKit):

3 + 4;

And it returned:

3 4
false

Then I tried 10 % 4 and Article->find(4), and it worked. But I must say you scared the hell out of me with my first test :-). It was like "Woooww! This is... shit :P.

Very helpful. Thank you very much!
Matt said on Jun 10, 2009
The form data is being treated as urlencoded, so PHP is automatically treating the + as a space. Will post a fix shortly. Thanks for pointing this out.
Craig said on Jun 10, 2009
teehee 3 + 4 is such a funny bug!!

can you replace views/elements/interactive_panel.ctp, line 2 with:

echo $form->create('Interactive', array('url' => array('admin' => false, 'controller' => 'interactive', 'action' => 'cmd'), 'id' => 'interactive-form'));

so i can use this great thing in my admins!
Matt said on Jun 10, 2009
Will do. I'll post an update when I get home from work.
Giuppe said on Jun 15, 2009
It would be *really* awesome if it could run cake shell tasks! :)
Arnold Roa said on Jun 15, 2009
Hi, this looks great but, i cant get it working, if i add it to my $components var in AppController:

var $components = array('Cookie','Acl','Auth','AuthExtension','Email','RequestHandler',array('DebugKit.Toolbar' => array('panels' => array('Interactive.interactive'))));

i got this error:

Warning (2): Illegal offset type [CORE\cake\libs\set.php, line 905]

if i manually add the panel to toolbar component from debug kit:

var $_defaultPanels = array('session', 'request', 'sqlLog', 'timer', 'log', 'memory', 'variables', 'Interactive.interactive');

i got this error:

Fatal error: Class 'Interactive.interactivePanel' not found in C:\dev\colombia\LugoHermanos\branches\tienda\app\plugins\debug_kit\controllers\components\toolbar.php on line 167

so i really dont know what to do =P please help!
Matt said on Jun 15, 2009
Are you using the 1.1 stable version of the DebugKit or trunk? Sounds like 1.1. Did you do the patch as described in the readme?
Arnold Roa said on Jun 22, 2009
hi, you are right i was using the wrong version of debugkit, now i solve it and i see the interactive tab but if i execute a command a just see appears this table:

Debug timer infoMessageStart Time
(ms)End Time (ms)Duration (ms)Core Processing (Derived)012081208Component initialization and startup1208
124335

and nothing else, i double check my core debug and is set to 2
Matt said on Jun 25, 2009
Do any commands work for you? Like basic stuff (1+1)?
Jose Diaz-Gonzalez said on Jun 16, 2009
Cloning thechaw's DebugKit as of 3 hours ago, with your latest master also cloned in and using the setup for DebugKit trunk, I don't seem to be able to execute any commands. Do you have screenshots of how it should look? Perhaps I have a javascript error, or trunk is broken right now. (Running the latest stable cake)
Matt said on Jun 16, 2009
I just updated my DebugKit to the latest and it seems to be ok. Are you getting any errors? What happens when you hit execute?
Jose Diaz-Gonzalez said on Jun 17, 2009
Here's a screenshot of the command I run and what actually happens.

http://img38.imageshack.us/img38/1629/debug.gif
Matt said on Jun 17, 2009
Hmmm...I don't see anything especially interesting in the screenshot. Can you run other commands? Like straight PHP or SQL? Or commands that use Cake core libs of convenience functions?
Jose Diaz-Gonzalez said on Jun 17, 2009
No other command will run. Perhaps it is because I have acl enabled on this application? It's a simple blog application that I created using the ACL tutorial. I've edited it a bit, but nothing to damaging. I can plug up the source somewhere if you want to take a look.
Matt said on Jun 17, 2009
Sure, if you want to zip it up and attach to an email you can send to matt -at- pseudocoder.com and I'll check it out.