Hi folks,
i have writen an email about some kind of command line interface for the
framework some time ago and now have made a few tests i want to share to
get some feedback.
I realy love the Reflection-API, so i thought about using the api to
provide some kind of source code browser for projects written with the
ZendFramework.
So if you don't remember some parameters of method xy of all classes in
your path you don't need to go to the file and open it anymore! You only
want to see the docblock for a certain class to get short description of
parameters - no problem anymore (if you don't take the api-docs into
consideration;)
For the test i'm just using basic reflection stuff so only the 'reflect
class' command is available.
$reflect class Some_Class_In_You_IncludePath(*)<enter>
(*) - for example Zend_Log
I've put other code like evaling php code also inside for test purposes.
For example
$eval echo "Test";<enter>
Try to eval (echo "Test";):
Test
The nice thing with eval in this use case is that all variables are
available after the eval, so you can write multiple eval lines like
$eval $msg="Harharhar";<enter>
$eval echo $msg;
which is realy cool for fast testing scenarios! To not allways have to
use the eval command... i think about another command like
'evalMultiLine' for multiline commands.
This can be usefull for fast testing scenarios. For online usage, maybe
the console would be better implemented in some form with output
I think there are many use cases
I'm for sure going to implement the "phpsource to UML" feature to be
able to convert the class informations into the xml format of for
example the free uml modeller Umbrella. So with this little and realy
simple tool you could make the uml diagrams fromm ALL your classes you
are use during your php5 prorgramming sessions.
The full code can be found at
http://byteshelter.org/public/projects/Byteshelter_Php5_Console or just
in case i've pasted the code also in the mail.
please let me know what you think about the basic idea, THE CODE AT THE
MOMENT IS terrible - IT'S JUST A TEST TO SEE WHAT'S POSSIBLE. i'm going
to use the normal framework for the app in the final implementation and
use Zend_GetOpt to parse the parameters.
i'm very interrested about our responses =)
best regards,
Steven Truppe
,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-.,-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.
-----------------------------------------------------------------------------------------------------------
,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-.,-,.-,.-,.-,.-,.-,.
Byteshelter_Php5_Console.php
<?php
/**
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*
*/
require_once 'Byteshelter_Php5_Cli.php';
$includes = array("../library/ZendFramework-1.0.1/library/");
$cli = new Byteshelter_Php5_Cli($includes);
$cli->main();
,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-.,-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.
-----------------------------------------------------------------------------------------------------------
,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-,.-.,-,.-,.-,.-,.-,.-,.
Byteshelter_Php5_Cli.php
<?php
/**
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*
*/
/**
* PHP5 Sourcecode Browser - Interactive php shell and more
*
* Interactive command line interface like gdb that uses the PHP5
Reflection-API
* to extract the sourcecode of PHP5 files.
*
* @author Steven Truppe
*/
class Byteshelter_Php5_Cli
{
/**
* Welcome text...
*/
CONST WELCOME_MSG = "
+-----------------------------------+
| Byteshelter PHP5 Console |
| Copyright (c) 2008, Steven Truppe |
+-----------------------------------+\n\n";
/**
* Keep looping until this is true
*/
protected $_quit = false;
/**
* User input is stored here
*/
protected $_input = null;
/**
* init class
*/
public function __construct($includes)
{
$this->setIncludePath($includes);
}
/**
* add a list of directories to the include path
*/
public function setIncludePath($add, $overwrite=false)
{
$oldPath = get_include_path();
foreach((array) $add as $new)
$oldPath .= ':'.$new;
set_include_path($oldPath);
}
/**
* proceed user input
*
* @todo use zf to use module / controller / actions for parameters
* @todo use Zend_GetOpt
*/
public function main()
{
while(!$this->_quit)
{
echo "Byteshelter_Php5_Cli$ ";
$line = trim(fgets(STDIN));
$params = explode(" ", $line);
switch(strtolower($params[0]))
{
case 'quit':
$this->_quit = true;
break;
case 'eval':
$cmd = substr($line, 5, strlen($line));
echo "Try to eval( $cmd )". PHP_EOL;
eval($cmd);
echo "\n";
break;
case 'ini_set':
if ($params[1] == 'include_path')
{
$ret =
set_include_path(get_include_path().':'.$params[2]);
echo "result = $ret\n";
}
else ini_set($params[1], $params[2]);
break;
case 'ini_get':
echo "ini_get(" . $params[1] . ")";
echo ini_get($params[1]);
echo PHP_EOL;
break;
/**
* reflect sources
*/
case 'reflect':
switch(strtolower($params[1]))
{
// reflect hole class
case 'class':
echo "[reflect class]". PHP_EOL;
// replace _ with / and
$classFile = str_replace("_", "/",
$params[2]).".php";
echo "Search for classfile $classFile\n";
require_once $classFile;
Reflection::export(new
ReflectionClass($params[2]));
echo "\n";
break;
// reflect function
case 'func':
// replace _ with / and
$classFile = str_replace("_", "/",
$params[2]).".php";
echo "Search for classfile $classFile\n";
require_once $classFile;
Reflection::export(new
ReflectionClass($params[2]));
echo "\n";
break;
// ??
default:break;
}
break;
// endof case 'reflect'
/**
* Command not found
*/
default:
echo "Command not found. Use 'man'.\n";
break;
}
}
}
}