Jump to content

Code Tester Module


adrian
 Share

Recommended Posts

This module is obsolete - please use the Console Panel in Tracy: https://adrianbj.github.io/TracyDebugger/#/debug-bar?id=console
 
Ok, this is really not very fancy, but I think it will come in quite handy - for me at least :)
 
It provides an admin page where you can test code without the need to edit template files.
 
It runs from a new item under Setup called "Code Tester"
 
Simply enter your code, including an opening <?php and click "Run Code".
 
Page will refresh with the code block still in editor, and with an iframe containing the results of your code below. In some cases the output may not be relevant if your code is manipulating pages etc via the API, rather than outputting content to a page.
 
Then you can easily make code changes and run again.
 
 
Installation
 
  • Install as normal, then move the included file "code_tester.php" to your site's templates folder
     
  • Depending on your sites template structure, you may want to edit this file. I have set it up using the head.inc and foot.inc approach that is used in PW's default profile. There are detailed instructions in the file to make it easy to modify as needed if you prefer using a single main.inc or other approach.
     
  • Install the ACE Text Editor module if you want syntax highlighting.
 
That's It!
 
 
How it works
 
  • Nothing very high tech - when the module installs it creates a new template: code_tester and a new unpublished page: Code Tester.
     
  • When you click "Run Code" it creates/updates this file: /site/assets/files/ID_of_Code_Tester_page/code_tester_code.php and writes your code to the file.
     
  • This file is included from the code_tester.php file and the code is run and the output processed.
     
  • Note that I am using ob_start(); include; $out = ob_get_clean(); so that this will work take the approach of populating variables that are output within a main/shared markup file like main.inc
     
  • If you want to use a variable other than $out in your test code, you will need to edit the code_tester.php file accordingly.
 
Things you can do
 
It may not be immediately obvious as to some of the things that work with this, so here are some examples:
echo 'test';
$out .= 'test';
//list page titles matching the selector
foreach($pages->find(selector) as $p) $out .= "<li>$p->title</li>";
//populate the results frame with the page returned by the selector. You must use $getpage as the variable!
$getpage = $pages->get(selector);
// bulk delete pages matching the selector
foreach($pages->find(selector) as $p) $p->delete();

So really it is more than a code tester - it can also be used as a way to test selectors and return the resulting page and also as a way of running admin maintenance scripts.

 
Hopefully you guys might find it useful.
 
Please let me know if you have any thoughts for improvements.

post-985-0-39666100-1381342096_thumb.png

post-985-0-84810200-1381342101_thumb.png

  • Like 8
Link to comment
Share on other sites

Just updated module with support for the ACE editor module so there is now syntax highlighting if you have it installed.

EDIT: Also support for populating the $out variable, rather than echo'ing out the content directly if you'd prefer (either works). You can change this to a different variable in the code_tester.php file.

Link to comment
Share on other sites

Just added support for being able to return a page based on a selector, eg:

//populate the results frame with the page returned by the selector. You must use $getpage as the variable!
$getpage = $pages->get(selector);

EDIT: Just added a code fix (in the included template file) and requirement for using $getpage as the variable to populate to avoid conflicts with normal code execution.

Edited by adrian
Link to comment
Share on other sites

Well I officially feel a little silly. I was thinking about adding the ability to store code snippets into code tester, and so I thought I'd check out how the Hanna Code module handles this, when I discovered that it also has the ability to run code directly from the admin using the test functionality :)

So after seeing that and how well it's implemented, I am wondering if there is a place for this module at all.

The few advantages that Code Tester has over the test functionality in Hanna are:

  • the ability to return the page from a selector query, eg: 
    $getpage = $pages->get(selector); 
    

    which I do think is very useful when these get more complicated and include chaining etc - ie things that can't be done with the Nik's selector test module.

  • display code results in context of the site's front-end look/structure
     
  • users might find this module before they find Hanna code if they are searching for a way to test their code
     
  • I was thinking about linking code tester to gist or the long talked about snippet directory: http://processwire.com/talk/topic/1018-repository-of-php-scripts-for-templates/

I'd still really love to see the snippets directory happen - Ryan, can we help with that? It would be great to have the code that runs the modules directory, rather than starting from scratch. Would you be open to having Hanna code access snippets from a directory directly from the Hanna interface?

Anyone have any thoughts?

Link to comment
Share on other sites

I'd still really love to see the snippets directory happen - Ryan, can we help with that? It would be great to have the code that runs the modules directory, rather than starting from scratch. Would you be open to having Hanna code access snippets from a directory directly from the Hanna interface?

Sounds good to me. But I haven't seen enough Hanna code snippets posted to make me think there would be much demand for a directory of them yet. 

Link to comment
Share on other sites

Sounds good to me. But I haven't seen enough Hanna code snippets posted to make me think there would be much demand for a directory of them yet. 

I actually mean code snippets in general, not just those that would be relevant for use with Hanna code.

There are so many useful bits of code spread throughout this forum and various gist repos (especially Soma's), but finding them can be difficult. The ability to be able to search for a function and execute it or copy it for use in a template directly from within a code tester/runner or the Hanna code module would be fantastic.

I could see that for snippets that perform admin type functions this could actually replace some some potential desire for smaller modules. An example might be your clean-files.php script: http://processwire.com/talk/topic/4437-delete-orphaned-filesimages-from-siteassetsfiles/?p=43687

I think it would be cool to be able to fire up the code runner and browse by category and/or search for "clean orphaned files" and see an explanation of what it does and then be able to run the script directly without the need for copying/pasting from the forum into a template and executing that.

With my page tree migrator I have done something like this with the ability to import files directly from the page-list git repo from a simple dropdown, so surely we could something like this using gists, but with searching options? 

  • Like 2
Link to comment
Share on other sites

I agree, it sounds useful. Maybe in the short term, we should just add a new board here called "Code Snippets" and perhaps have a few categories under it? Once there's a good supply of stuff in there, we could make a more dedicated platform for it. 

Link to comment
Share on other sites

I'd be happy to help with the code snippets idea. I already own codemorsels.com for exactly that purpose. Might be nice to have an online repository along with a PW module that can access those snippets directly. I'm also building a theme framework for PW and thought a snippet system would be nice for that. If you take the lead I'll assist. 

Link to comment
Share on other sites

Thinking about this a little further. I wonder how difficult it would be to construct complete modules with a GUI and snippets. For example, the module builder would let you choose the hook or none, then it would build the method from the snippet and name you choose. Kind of like using PHP packages http://getcomposer.org/. 

Link to comment
Share on other sites

Hi sshaw,

Thanks for your enthusiasm on this. I am still trying to figure out what I think is the best approach here. Here are the functionalities on my list that I would like to see:

  1. Code snippet repository that anyone can contribute to
     
  2. Snippets can be "installed/updated" on your site with one click
     
  3. An admin interface that handles running these snippets in different ways, so no need to copy/paste code anywhere.
    1. On-demand code execution - for admin type tasks, such as Ryan's clean-files.php script mentioned above
    2. Triggered by a specific PW hook
    3. Injected into specific template files
    4. Embedded into an RTE the way the Hanna Code works

As I mentioned above, I think this could really remove the need for module creation in many cases.

I wonder if this needs to be a new module, or if extending Hanna Code would work best.

PS Just read your last post properly - sounds like we are on the same wavelength here :)

I am definitely happy to take the lead on this, although I'd really like to get some input from the PW gurus on this - in particular Ryan, what do you think about the idea of using Hanna Code as a base for this. I don't want to bloat it with things some users won't want, but I also don't want to have a completely separate module to store snippets that can't be embedded like Hanna does. Maybe the new module could just hook into the Hanna snippets DB table. Any thoughts?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...