Skip to content
This repository has been archived by the owner on Nov 24, 2019. It is now read-only.

Commit

Permalink
add fs cli command
Browse files Browse the repository at this point in the history
add 'fs' CLI command for generate a whole module. example: php fs g
MyModule
  • Loading branch information
ve3 committed Nov 23, 2014
1 parent ea659f6 commit 7b74cff
Show file tree
Hide file tree
Showing 11 changed files with 951 additions and 2 deletions.
56 changes: 56 additions & 0 deletions fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* The FuelStart Fs cli was copied from FuelPhp Oil.
*/

/**
* Refuse to run oil when called from php-cgi !
*/
if (substr(php_sapi_name(), 0, 3) == 'cgi')
{
die("The use of cli is not supported when running php-cgi. CLI needs php-cli to function!\n\n");
}

/**
* Set error reporting and display errors settings. You will want to change these when in production.
*/
error_reporting(-1);
ini_set('display_errors', 1);

/**
* Website document root
*/
define('DOCROOT', __DIR__.DIRECTORY_SEPARATOR);

/**
* Path to the application directory.
*/
define('APPPATH', realpath(__DIR__.'/fuel/app/').DIRECTORY_SEPARATOR);

/**
* Path to the default packages directory.
*/
define('PKGPATH', realpath(__DIR__.'/fuel/packages/').DIRECTORY_SEPARATOR);

/**
* The path to the framework core.
*/
define('COREPATH', realpath(__DIR__.'/fuel/core/').DIRECTORY_SEPARATOR);

// Get the start time and memory for use later
defined('FUEL_START_TIME') or define('FUEL_START_TIME', microtime(true));
defined('FUEL_START_MEM') or define('FUEL_START_MEM', memory_get_usage());

// Load in the Fuel autoloader
require COREPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php';
class_alias('Fuel\\Core\\Autoloader', 'Autoloader');

// Boot the app
require APPPATH.'bootstrap.php';

Package::load('fs');

// Fire up the command line interfact
Fs\Command::init($_SERVER['argv']);

/* End of file oil */
1 change: 1 addition & 0 deletions fuel/app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Autoloader::add_classes(array(
// Add classes you want to override here
// Example: 'View' => APPPATH.'classes/view.php',
'Theme' => APPPATH.'classes/extension/theme.php',
// @link http://www.marcopace.it/blog/2012/12/fuelphp-i18n-internationalization-of-a-web-application tutorial i18n.
'Uri' => APPPATH.'classes/extension/uri.php',
));
Expand Down
84 changes: 84 additions & 0 deletions fuel/app/classes/extension/theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Extend theme class
*
* @author Vee W.
*/

class Theme extends \Fuel\Core\Theme
{


public function __construct(array $config = array()) {
parent::__construct($config);
}// __construct


/**
* find view file<br>
* this method that extends fuelphp core theme is for re-arrange priority of theme and views.
*
* @param string $view
* @param string $themes
* @return string
*/
protected function find_file($view, $themes = null)
{
if ($themes === null) {
$themes = array($this->active, $this->fallback);
}

// determine the path prefix and optionally the module path
$path_prefix = '';
$module_path = null;
if ($this->config['use_modules'] and class_exists('Request', false) and $request = \Request::active() and $module = $request->module) {
// we're using module name prefixing
$path_prefix = $module.DS;

// and modules are in a separate path
is_string($this->config['use_modules']) and $path_prefix = trim($this->config['use_modules'], '\\/').DS.$path_prefix;

// do we need to check the module too?
$this->config['use_modules'] === true and $module_path = \Module::exists($module).'themes'.DS;
}

foreach ($themes as $theme) {
$ext = pathinfo($view, PATHINFO_EXTENSION) ?
'.'.pathinfo($view, PATHINFO_EXTENSION) : $this->config['view_ext'];
$file = (pathinfo($view, PATHINFO_DIRNAME) ?
str_replace(array('/', DS), DS, pathinfo($view, PATHINFO_DIRNAME)).DS : '').
pathinfo($view, PATHINFO_FILENAME);

if (empty($theme['find_file'])) {
if ($module_path and ! empty($theme['name']) and is_file($path = $module_path.$theme['name'].DS.$file.$ext)) {
// if use_modules is true then this $path will be /www/root/modules/<module name>/themes/<theme name>/<$view>.php
return $path;
} elseif (is_file($path = $theme['path'].$path_prefix.$file.$ext)) {
// if use_modules is true then $path will be /www/root/<theme path>/<theme name>/<module name>/<$view>.php
// if use_modules is 'modules' then $path will be /www/root/<theme path>/<theme name>/modules/<module name>/<$view>.php
return $path;
} elseif (is_file($path = \Module::exists($module) . 'views' . DS . $file . $ext)) {
/**
* this condition was added by Vee W.
* look directly in modules/module_name/views. this $path will be /www/root/<modules path>/<module name>/views/<$view>.php
*
* @author Vee W.
*/
return $path;
} elseif (is_file($path = $theme['path'].$file.$ext)) {
// this will not look into module name anymore. $path will be /www/root/<theme path>/<theme name>/<$view>.php
return $path;
}
} else {
if ($path = \Finder::search($theme['path'].$path_prefix, $file, $ext)) {
return $path;
}
}
}

// not found, return the viewname to fall back to the standard View processing
return $view;
}// find_file


}
2 changes: 1 addition & 1 deletion fuel/app/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*/
// 'index_file' => false,

'profiling' => true,
'profiling' => false,

/**
* Default location for the file cache
Expand Down
1 change: 1 addition & 0 deletions fuel/app/lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

$lang['admin_add'] = 'Add';
$lang['admin_administrator_dashbord'] = 'Administrator dashboard';
$lang['admin_are_you_sure_to_delete_selected_items'] = 'Are you sure to delete selected items?';
$lang['admin_cancel'] = 'Canel';
$lang['admin_delete'] = 'Delete';
$lang['admin_disable'] = 'Disable';
Expand Down
1 change: 1 addition & 0 deletions fuel/app/lang/th/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

$lang['admin_add'] = 'เพิ่ม';
$lang['admin_administrator_dashbord'] = 'หน้าแรกผู้ดูแล';
$lang['admin_are_you_sure_to_delete_selected_items'] = 'คุณแน่ใจหรือไม่ที่จะลบรายการที่เลือก?';
$lang['admin_cancel'] = 'ยกเลิก';
$lang['admin_delete'] = 'ลบ';
$lang['admin_disable'] = 'ปิดใช้งาน';
Expand Down
7 changes: 7 additions & 0 deletions fuel/packages/fs/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

Autoloader::add_classes(array(
'Fs\\Command' => __DIR__.'/classes/command.php',
'Fs\\Exception' => __DIR__.'/classes/exception.php',
'Fs\\Generate' => __DIR__.'/classes/generate.php',
));
102 changes: 102 additions & 0 deletions fuel/packages/fs/classes/command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Fs;

/**
* FuelStart CLI command
*
* @author Vee W.
*
* The FuelStart Fs cli was copied from FuelPhp Oil.
*/
class Command
{


protected static function _clear_args($actions = array())
{
foreach ($actions as $key => $action)
{
if (substr($action, 0, 1) === '-')
{
unset($actions[$key]);
}

// get rid of any junk added by Powershell on Windows...
isset($actions[$key]) and $actions[$key] = trim($actions[$key]);
}

return $actions;
}// _clear_args


public static function help()
{
echo <<<HELP
Usage:
php fs [g|generate] [module_name]
[generate] is generete the module.
[module_name] is the module name. It should be StudlyCaps as PSR-1 specified.
Description:
The 'fs' is FuelStart command, It will be use for generate module that is ready to code.
HELP;
}// help


public static function init($args)
{
// Remove flag options from the main argument list
$args = self::_clear_args($args);

try {
if (!isset($args[1])) {
static::help();
return false;
}

switch ($args[1]) {
case 'g':
case 'generate':
if (!isset($args[2])) {
\Cli::error('Please enter module name.');
\Cli::beep();
return false;
}

call_user_func('Fs\Generate::module', array_slice($args, 2));
break;
default:
static::help();
}
} catch (\Exception $e) {
static::print_exception($e);
exit(1);
}// end try catch
}// init


protected static function print_exception(\Exception $ex)
{
\Cli::error('Uncaught exception '.get_class($ex).': '.$ex->getMessage());
if (\Fuel::$env != \Fuel::PRODUCTION)
{
\Cli::error('Callstack: ');
\Cli::error($ex->getTraceAsString());
}
\Cli::beep();
\Cli::option('speak') and `say --voice="Trinoids" "{$ex->getMessage()}"`;

if (($previous = $ex->getPrevious()) != null)
{
\Cli::error('');
\Cli::error('Previous exception: ');
static::print_exception($previous);
}
}


}
13 changes: 13 additions & 0 deletions fuel/packages/fs/classes/exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Fs;

class Exception extends \Exception
{

// public function __toString()
// {
// \Cli::write('Error: ' . $this->message);
// }

}
Loading

0 comments on commit 7b74cff

Please sign in to comment.