示例#1
0
文件: CLI.php 项目: HuangStomach/gini
 public static function possibleCommands($argv)
 {
     // list available cli programs
     $candidates = ['/' => []] + Util::pathAndArgs($argv, true);
     $commands = [];
     $class = null;
     foreach (array_reverse($candidates) as $path => $params) {
         $paths = \Gini\Core::pharFilePaths(CLASS_DIR, rtrim('Gini/Controller/CLI/' . ltrim($path, '/'), '/'));
         foreach ($paths as $p) {
             if (!is_dir($p)) {
                 continue;
             }
             \Gini\File::eachFilesIn($p, function ($file) use(&$commands) {
                 $name = basename(strtolower(explode('/', $file, 2)[0]), '.php');
                 $commands[$name] = $name;
             });
         }
         // enumerate actions in class
         $path = strtr(ltrim($path, '/'), ['-' => '', '_' => '']);
         $basename = basename($path);
         $dirname = dirname($path);
         $class_namespace = '\\Gini\\Controller\\CLI\\';
         if ($dirname != '.') {
             $class_namespace .= strtr($dirname, ['/' => '\\']) . '\\';
         }
         $class = $class_namespace . $basename;
         if (class_exists($class)) {
             break;
         }
         $class = $class_namespace . 'Controller' . $basename;
         if (class_exists($class)) {
             break;
         }
         $class = null;
     }
     if (!$class) {
         $class = '\\Gini\\Controller\\CLI\\App';
         $params = $argv;
     }
     if (class_exists($class)) {
         $rc = new \ReflectionClass($class);
         $methods = $rc->getMethods(\ReflectionMethod::IS_PUBLIC);
         foreach ($methods as $m) {
             if (strncmp('action', $m->name, 6) != 0) {
                 continue;
             }
             if (preg_match_all('`([A-Z]+[a-z\\d]+|.+)`', substr($m->name, 6), $parts)) {
                 $method = strtolower(implode('-', $parts[0]));
                 if ($params[0] === $method) {
                     $commands = [];
                     break;
                 }
                 $commands[] = $method;
             }
         }
     }
     return $commands;
 }
示例#2
0
文件: ORM.php 项目: HuangStomach/gini
 public function actionExport($args)
 {
     printf("Exporting ORM structures...\n\n");
     $orm_dirs = \Gini\Core::pharFilePaths(CLASS_DIR, 'Gini/ORM');
     foreach ($orm_dirs as $orm_dir) {
         if (!is_dir($orm_dir)) {
             continue;
         }
         \Gini\File::eachFilesIn($orm_dir, function ($file) use($orm_dir) {
             $oname = strtolower(preg_replace('|.php$|', '', $file));
             if ($oname == 'object') {
                 return;
             }
             $class_name = '\\Gini\\ORM\\' . str_replace('/', '\\', $oname);
             // Check if it is abstract class
             $rc = new \ReflectionClass($class_name);
             if ($rc->isAbstract()) {
                 return;
             }
             printf("   %s\n", $oname);
             $o = \Gini\IoC::construct($class_name);
             $structure = $o->structure();
             // unset system fields
             unset($structure['id']);
             unset($structure['_extra']);
             $i = 1;
             $max = count($structure);
             foreach ($structure as $k => $v) {
                 if ($i == $max) {
                     break;
                 }
                 printf("   ├─ %s (%s)\n", $k, implode(',', array_map(function ($k, $v) {
                     return $v ? "{$k}:{$v}" : $k;
                 }, array_keys($v), $v)));
                 ++$i;
             }
             printf("   └─ %s (%s)\n\n", $k, implode(',', array_map(function ($k, $v) {
                 return $v ? "{$k}:{$v}" : $k;
             }, array_keys($v), $v)));
         });
     }
 }
示例#3
0
 private static function _getORMPlurals()
 {
     $orm_dirs = \Gini\Core::pharFilePaths(CLASS_DIR, 'Gini/ORM');
     $onames = [];
     foreach ($orm_dirs as $orm_dir) {
         if (!is_dir($orm_dir)) {
             continue;
         }
         \Gini\File::eachFilesIn($orm_dir, function ($file) use($orm_dir, &$onames) {
             $oname = preg_replace('|.php$|', '', $file);
             if ($oname == 'Object') {
                 return;
             }
             $class_name = '\\Gini\\ORM\\' . str_replace('/', '\\', $oname);
             // Check if it is abstract class
             $rc = new \ReflectionClass($class_name);
             if ($rc->isAbstract()) {
                 return;
             }
             $onames[] = strtolower($oname);
         });
     }
     if (count($onames) == 0) {
         return [];
     }
     printf("%s\n", 'Generating ORM plurals cache...');
     if (!class_exists('\\Doctrine\\Common\\Inflector\\Inflector')) {
         echo "    * doctrine/inflector is missing! Perhaps you did't update your composer packages yet.\n\n";
         return [];
     }
     $plurals = [];
     $onames = array_unique($onames);
     foreach ($onames as $oname) {
         $plural = \Doctrine\Common\Inflector\Inflector::pluralize($oname);
         printf("   %s => %s\n", $plural, $oname);
         $plurals[$plural] = $oname;
     }
     echo "\n";
     return $plurals;
 }
示例#4
0
文件: Config.php 项目: iamfat/gini
 public static function fetch($env = null)
 {
     $env = $env ?: APP_PATH . '/.env';
     if (file_exists($env)) {
         $rows = file($env, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
         foreach ($rows as &$row) {
             if (!$row || $row[0] == '#') {
                 continue;
             }
             putenv($row);
         }
     }
     $items = [];
     $paths = \Gini\Core::pharFilePaths(RAW_DIR, 'config');
     foreach ($paths as $path) {
         self::_load_config_dir($path, $items);
     }
     $env = $_SERVER['GINI_ENV'];
     if ($env) {
         $paths = \Gini\Core::pharFilePaths(RAW_DIR, 'config/@' . $env);
         foreach ($paths as $path) {
             self::_load_config_dir($path, $items);
         }
     }
     return $items;
 }
示例#5
0
文件: App.php 项目: HuangStomach/gini
 public function actionWatch($args)
 {
     $watcher = new \Lurker\ResourceWatcher(in_array('-r', $args) ? new \Lurker\Tracker\RecursiveIteratorTracker() : null);
     // Config
     $paths = \Gini\Core::pharFilePaths(RAW_DIR, 'config');
     array_walk($paths, function ($path) use($watcher) {
         $watcher->trackByListener($path, function (\Lurker\Event\FilesystemEvent $event) {
             passthru('gini config update');
         });
     });
     // Class && View
     $paths = array_merge(\Gini\Core::pharFilePaths(CLASS_DIR, ''), \Gini\Core::pharFilePaths(VIEW_DIR, ''));
     array_walk($paths, function ($path) use($watcher) {
         $watcher->trackByListener($path, function (\Lurker\Event\FilesystemEvent $event) {
             passthru('gini cache');
         });
     });
     // ORM
     $paths = \Gini\Core::pharFilePaths(CLASS_DIR, 'Gini/ORM');
     array_walk($paths, function ($path) use($watcher) {
         $watcher->trackByListener($path, function (\Lurker\Event\FilesystemEvent $event) {
             passthru('gini orm update');
         });
     });
     // Web
     $paths = array_merge(\Gini\Core::pharFilePaths(RAW_DIR, 'assets'), \Gini\Core::pharFilePaths(RAW_DIR, 'js'), \Gini\Core::pharFilePaths(RAW_DIR, 'less'));
     array_walk($paths, function ($path) use($watcher) {
         $watcher->trackByListener($path, function (\Lurker\Event\FilesystemEvent $event) {
             passthru('gini web update');
         });
     });
     echo "watching config/class/view/orm/web...\n";
     $watcher->start();
 }
示例#6
0
 public function testPharFilePaths()
 {
     $paths = \Gini\Core::pharFilePaths(CLASS_DIR, 'Gini/Core.php');
     $this->assertTrue(in_array(SYS_PATH . '/class/Gini/Core.php', $paths));
 }
示例#7
0
文件: ORM.php 项目: iamfat/gini
 public function actionUpgradeId()
 {
     // ORM required class map.
     if (!isset($GLOBALS['gini.class_map'])) {
         echo "You need to run \"gini cache class\" before upgrade ORM id.\n";
         return;
     }
     // enumerate orms
     echo "Updating database structures according ORM definition...\n";
     $orm_dirs = \Gini\Core::pharFilePaths(CLASS_DIR, 'Gini/ORM');
     foreach ($orm_dirs as $orm_dir) {
         if (!is_dir($orm_dir)) {
             continue;
         }
         \Gini\File::eachFilesIn($orm_dir, function ($file) use($orm_dir) {
             $oname = preg_replace('|.php$|', '', $file);
             if ($oname == 'Object') {
                 return;
             }
             $class_name = '\\Gini\\ORM\\' . str_replace('/', '\\', $oname);
             // Check if it is abstract class
             $rc = new \ReflectionClass($class_name);
             if ($rc->isAbstract()) {
                 return;
             }
             $o = \Gini\IoC::construct($class_name);
             // some object might not have database backend
             $db = $o->db();
             if (!$db) {
                 return;
             }
             printf("   %s\n", $oname);
             $structure = $o->structure();
             foreach ($structure as $field => $option) {
                 if (isset($option['object'])) {
                     $db->query('UPDATE :table SET :field=NULL WHERE :field=0', [':table' => $o->tableName(), ':field' => $field . '_id']);
                 }
             }
         });
     }
     echo "   done.\n";
 }
示例#8
0
 public static function fetch($env = null)
 {
     $items = [];
     $paths = \Gini\Core::pharFilePaths(RAW_DIR, 'config');
     foreach ($paths as $path) {
         self::_load_config_dir($path, $items);
     }
     $env = $env ?: $_SERVER['GINI_ENV'] ?: '';
     if ($env) {
         $paths = \Gini\Core::pharFilePaths(RAW_DIR, 'config/@' . $env);
         foreach ($paths as $path) {
             self::_load_config_dir($path, $items);
         }
     }
     return $items;
 }