]> git.andy128k.dev Git - ipf.git/commitdiff
debug server and display routes commands
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 20 Jun 2013 22:32:50 +0000 (01:32 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 20 Jun 2013 22:32:50 +0000 (01:32 +0300)
index.php
ipf.php
ipf/cli.php
ipf/command/debugserver.php [new file with mode: 0644]
ipf/command/routes.php [new file with mode: 0644]
ipf/router.php
ipf/shell.php

index 444325d17dfef8bd2ad1f4870ae44e77f737d30c..93fb5ce66e406332dd0e4ec845d50b88c4c57757 100644 (file)
--- a/index.php
+++ b/index.php
@@ -2,9 +2,10 @@
 
 // This is a index stub for a IPF Projects
 
-$ipf_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'ipf';
-$project_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'project';
+$here = dirname(__FILE__);
+$ipf_path = $here.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'ipf';
+$project_path = $here.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'project';
 set_include_path(get_include_path() . PATH_SEPARATOR . $ipf_path . PATH_SEPARATOR . $project_path);
 require 'ipf.php';
-return IPF::boot($ipf_path, $project_path) && IPF_Project::getInstance()->run();
+return IPF::boot($ipf_path, $project_path, $here) && IPF_Project::getInstance()->run();
 
diff --git a/ipf.php b/ipf.php
index 3281b9f5ed9f9d63d6e4a42394b457f53cd9979c..2edf6c8cc32200389f054ebcf3192d06611955d0 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -112,13 +112,14 @@ final class IPF
         return is_file($path);
     }
 
-    public static function boot($ipf_path, $project_path)
+    public static function boot($ipf_path, $project_path, $document_root)
     {
         if (php_sapi_name() === 'cli-server' && IPF::requestedFileExists())
             return false;
 
         IPF::$settings['ipf_path'] = $ipf_path;
         IPF::$settings['project_path'] = $project_path;
+        IPF::$settings['document_root'] = $document_root;
 
         try {
             IPF::loadSettings();
index 37c5f1acbfa5b127bd9e3ef254a5d6757f69f82a..eff7869f7f90a602b51d413d9b3739fd277615c0 100644 (file)
@@ -7,6 +7,8 @@ class IPF_Cli
     public function __construct()
     {
         $this->commands = array(
+            new IPF_Command_DebugServer,
+            new IPF_Command_Routes,
             new IPF_Command_BuildModels,
             new IPF_Command_BuildContribModels,
             new IPF_Command_Sql,
@@ -29,15 +31,15 @@ class IPF_Cli
         print "Usage: php index.php <subcommand> [options] [args]\n\n";
         print "Available subcommands:\n";
 
-        $firstColumnSize = 7;
-        foreach ($this->commands as $command) {
-            $l = strlen($command->command);
-            if ($l > $firstColumnSize)
-                $firstColumnSize = $l;
-        }
-        foreach ($this->commands as $command) {
-            print '    '.str_pad($command->command, $firstColumnSize) . "\t" . $command->description . "\n";
-        }
+        $rows = array();
+        foreach ($this->commands as $command)
+            $rows[] = array(
+                '    ' . $command->command,
+                $command->description,
+            );
+
+        IPF_Shell::displayTwoColumns($rows);
+
         print "\n";
     }
 
diff --git a/ipf/command/debugserver.php b/ipf/command/debugserver.php
new file mode 100644 (file)
index 0000000..07bc79e
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+class IPF_Command_DebugServer
+{
+    public $command = 'run';
+    public $description = 'Run debug server on 0.0.0.0:8000';
+
+    public function run($args=null)
+    {
+        $root = IPF::get('document_root');
+        IPF_Shell::call('php', '-S', '0.0.0.0:8000', '-t', $root, $root . '/index.php');
+    }
+}
+
diff --git a/ipf/command/routes.php b/ipf/command/routes.php
new file mode 100644 (file)
index 0000000..acbbf85
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+class IPF_Command_Routes
+{
+    public $command = 'routes';
+    public $description = 'Displays all routes';
+
+    public function run($args=null)
+    {
+        $rows = IPF_Router::describe();
+        IPF_Shell::displayTwoColumns($rows);
+    }
+}
+
index d19d3c4aa72740076248abab46db222b9b75e47d..deb1210226e1ba371705e4d549a2d74b94345799 100644 (file)
@@ -77,5 +77,20 @@ class IPF_Router
         }
         return new IPF_HTTP_Response_NotFound();
     }
+
+    public static function describe()
+    {
+        $routes = array();
+        foreach (IPF::get('urls') as $url) {
+            $prefix = $url['prefix'];
+            foreach ($url['urls'] as $suburl) {
+                $routes[] = array(
+                    $prefix . $suburl['regex'],
+                    $suburl['func'],
+                );
+            }
+        }
+        return $routes;
+    }
 }
 
index f29db2717ff887ca35ce2ea9409131cca75e5eb2..5ce5e2f968bf24cf0cac9bb1895cb08442bc8da7 100644 (file)
@@ -21,5 +21,20 @@ class IPF_Shell
         $process = proc_open($str, $descriptorspec, $pipes);
         proc_close($process);
     }
+
+    public static function displayTwoColumns($rows, $firstColumnMin=7, $firstColumnMax=47)
+    {
+        $firstColumnSize = $firstColumnMin;
+        foreach ($rows as $row) {
+            $l = strlen($row[0]);
+            if ($l > $firstColumnSize)
+                $firstColumnSize = $l;
+        }
+        if ($firstColumnSize > $firstColumnMax)
+            $firstColumnSize = $firstColumnMax;
+        foreach ($rows as $row) {
+            echo str_pad($row[0], $firstColumnSize) . "\t" . $row[1] . "\n";
+        }
+    }
 }