Eclipse で FuelPHP のコード補完を行わせる冴えたやり方

FuelPHP はコーディングの利便性を考えて、Fuel\Core 名前空間にあるクラスは、グローバル空間からも呼べるようになっています。

つまり、

Fuel\Core\Validation

と打たずに

Validation

あるいは

\Validation

で済むようになっています。

そのため、IDE でのメソッドの補完がそのままでは効きません。


コード補完のやり方

以下のファイルをプロジェクトのルートに置きます。

autocomplete.php

<?php

class Asset_Instance extends Fuel\Core\Asset_Instance {}
class Cache_Handler_Driver extends Fuel\Core\Cache_Handler_Driver {}
class Cache_Handler_Json extends Fuel\Core\Cache_Handler_Json {}
class Cache_Handler_Serialized extends Fuel\Core\Cache_Handler_Serialized {}
class Cache_Handler_String extends Fuel\Core\Cache_Handler_String {}
class Cache_Storage_Apc extends Fuel\Core\Cache_Storage_Apc {}
class Cache_Storage_Driver extends Fuel\Core\Cache_Storage_Driver {}
class Cache_Storage_File extends Fuel\Core\Cache_Storage_File {}
class Cache_Storage_Memcached extends Fuel\Core\Cache_Storage_Memcached {}
class Cache_Storage_Redis extends Fuel\Core\Cache_Storage_Redis {}
class Config_File extends Fuel\Core\Config_File {}
class Config_Ini extends Fuel\Core\Config_Ini {}
class Config_Interface extends Fuel\Core\Config_Interface {}
class Config_Json extends Fuel\Core\Config_Json {}
class Config_Php extends Fuel\Core\Config_Php {}
class Config_Yml extends Fuel\Core\Config_Yml {}
class Controller_Rest extends Fuel\Core\Controller_Rest {}
class Controller_Template extends Fuel\Core\Controller_Template {}
class Database_Mysql_Connection extends Fuel\Core\Database_Mysql_Connection {}
class Database_Mysql_Result extends Fuel\Core\Database_Mysql_Result {}
class Database_Mysqli_Connection extends Fuel\Core\Database_Mysqli_Connection {}
class Database_Mysqli_Result extends Fuel\Core\Database_Mysqli_Result {}
class Database_Pdo_Connection extends Fuel\Core\Database_Pdo_Connection {}
class Database_Query_Builder_Delete extends Fuel\Core\Database_Query_Builder_Delete {}
class Database_Query_Builder_Insert extends Fuel\Core\Database_Query_Builder_Insert {}
class Database_Query_Builder_Join extends Fuel\Core\Database_Query_Builder_Join {}
class Database_Query_Builder_Select extends Fuel\Core\Database_Query_Builder_Select {}
class Database_Query_Builder_Update extends Fuel\Core\Database_Query_Builder_Update {}
class Database_Query_Builder_Where extends Fuel\Core\Database_Query_Builder_Where {}
class Database_Query_Builder extends Fuel\Core\Database_Query_Builder {}
class Database_Result_Cached extends Fuel\Core\Database_Result_Cached {}
class Database_Connection extends Fuel\Core\Database_Connection {}
class Database_Exception extends Fuel\Core\Database_Exception {}
class Database_Expression extends Fuel\Core\Database_Expression {}
class Database_Query extends Fuel\Core\Database_Query {}
class Database_Result extends Fuel\Core\Database_Result {}
class Database_Transaction extends Fuel\Core\Database_Transaction {}
class Fieldset_Field extends Fuel\Core\Fieldset_Field {}
class File_Handler_Directory extends Fuel\Core\File_Handler_Directory {}
class File_Handler_File extends Fuel\Core\File_Handler_File {}
class File_Area extends Fuel\Core\File_Area {}
class Form_Instance extends Fuel\Core\Form_Instance {}
class Image_Driver extends Fuel\Core\Image_Driver {}
class Image_Gd extends Fuel\Core\Image_Gd {}
class Image_Imagemagick extends Fuel\Core\Image_Imagemagick {}
class Image_Imagick extends Fuel\Core\Image_Imagick {}
class Model_Crud extends Fuel\Core\Model_Crud {}
class Mongo_Db extends Fuel\Core\Mongo_Db {}
class Request_Curl extends Fuel\Core\Request_Curl {}
class Request_Driver extends Fuel\Core\Request_Driver {}
class Request_Soap extends Fuel\Core\Request_Soap {}
class Session_Cookie extends Fuel\Core\Session_Cookie {}
class Session_Db extends Fuel\Core\Session_Db {}
class Session_Driver extends Fuel\Core\Session_Driver {}
class Session_Exception extends Fuel\Core\Session_Exception {}
class Session_File extends Fuel\Core\Session_File {}
class Session_Memcached extends Fuel\Core\Session_Memcached {}
class Session_Redis extends Fuel\Core\Session_Redis {}
class Validation_Error extends Fuel\Core\Validation_Error {}
class Agent extends Fuel\Core\Agent {}
class Arr extends Fuel\Core\Arr {}
class Asset extends Fuel\Core\Asset {}
class Autoloader extends Fuel\Core\Autoloader {}
class Cache extends Fuel\Core\Cache {}
class Cli extends Fuel\Core\Cli {}
class Config extends Fuel\Core\Config {}
class Controller extends Fuel\Core\Controller {}
class Cookie extends Fuel\Core\Cookie {}
class Crypt extends Fuel\Core\Crypt {}
class Date extends Fuel\Core\Date {}
class Db extends Fuel\Core\Db {}
class Dbutil extends Fuel\Core\Dbutil {}
class Debug extends Fuel\Core\Debug {}
class Error extends Fuel\Core\Error {}
class Event extends Fuel\Core\Event {}
class Fieldset extends Fuel\Core\Fieldset {}
class File extends Fuel\Core\File {}
class Finder extends Fuel\Core\Finder {}
class Form extends Fuel\Core\Form {}
class Format extends Fuel\Core\Format {}
class Ftp extends Fuel\Core\Ftp {}
class Fuel extends Fuel\Core\Fuel {}
class Html extends Fuel\Core\Html {}
class Httpexception extends Fuel\Core\Httpexception {}
class Httpexceptions extends Fuel\Core\Httpexceptions {}
class Image extends Fuel\Core\Image {}
class Inflector extends Fuel\Core\Inflector {}
class Input extends Fuel\Core\Input {}
class Lang extends Fuel\Core\Lang {}
class Log extends Fuel\Core\Log {}
class Markdown extends Fuel\Core\Markdown {}
class Migrate extends Fuel\Core\Migrate {}
class Model extends Fuel\Core\Model {}
class Num extends Fuel\Core\Num {}
class Package extends Fuel\Core\Package {}
class Pagination extends Fuel\Core\Pagination {}
class Profiler extends Fuel\Core\Profiler {}
class Redis extends Fuel\Core\Redis {}
class Request extends Fuel\Core\Request {}
class Response extends Fuel\Core\Response {}
class Route extends Fuel\Core\Route {}
class Router extends Fuel\Core\Router {}
class Security extends Fuel\Core\Security {}
class Session extends Fuel\Core\Session {}
class Str extends Fuel\Core\Str {}
class Testcase extends Fuel\Core\Testcase {}
class Theme extends Fuel\Core\Theme {}
class Unzip extends Fuel\Core\Unzip {}
class Upload extends Fuel\Core\Upload {}
class Uri extends Fuel\Core\Uri {}
class Validation extends Fuel\Core\Validation {}
class View extends Fuel\Core\View {}
class Viewmodel extends Fuel\Core\Viewmodel {}

これでメソッドの自動補完も効くようになります。


autocomplete.php を自動生成

上記の autocomplete.php は、クラスが増えたりすると変更しないといけないわけですが、それを自動生成するタスクです。

(2014/02/25) https://github.com/kenjis/fuelphp-tools/blob/master/app/tasks/generate.php に最新版があります。

<?php

namespace Fuel\Tasks;

class Generate
{
	static $class_definition = '';
	
	public static function run()
	{
		echo <<<EOL
Usage:
  oil refine generate:autocomplete  ... generate php file for IDE's auto completion
EOL;
	}
	
	public static function autocomplete()
	{
		$classes = \File::read_dir(COREPATH . 'classes');
		static::generate_class_definition($classes);
		static::$class_definition = '<?php' . "\n\n" . static::$class_definition;
		
		$file = 'autocomplete.php';
		$ret = file_put_contents($file, static::$class_definition);
		if ($ret === false)
		{
			echo 'Can\'t write to ' . $file;
		}
		else
		{
			echo $file . ' was created.';
		}
	}
	
	private static function generate_class_definition($classes, $str = '')
	{
		foreach ($classes as $dir => $file)
		{
			if (is_array($file))
			{
				static::generate_class_definition($file, $str . ucfirst(rtrim($dir, '/') . '_'));
			}
			else
			{
				$class_name = $str . ucfirst(str_replace('.php', '', $file));
				static::$class_definition .= 'class ' . $class_name;
				static::$class_definition .=  ' extends Fuel\\Core\\' . $class_name;
				static::$class_definition .=  ' {}' . "\n";
			}
		}
	}
}

/* End of file tasks/generate.php */