教程地址及说明
https://github.com/topdown/phpStorm-CC-Helpers
my_models文件生成工具
- 新建 application/controllers/test/Cli.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Cli extends CI_Controller {
function filename($path=APPPATH . 'models/', $exten = '.php', $ifchild = true){
static $file_array=array();
$path = preg_replace('/(.*)([^\/])$/', '$1$2/', $path);
if (is_dir($path)) {
$H = @ opendir($path);
while(false !== ($_file=readdir($H))){
if(is_dir($path.$_file) && $_file != "." && $_file!=".."){
if($ifchild){
$this->filename($path.$_file, $exten ,$ifchild);
}
}elseif(is_file($path.$_file) && $_file!="." && $_file!=".."){
if($exten == '*'){
array_push($file_array, $_file);
} else {
if(preg_match('/(.*)'.$exten.'/', '/'.$_file.'/')){
array_push($file_array, $_file);
}
}
}
}
closedir($H);
}
$array = $file_array;
return $array;
}
function create_my_models(){
$content_header = <<< 'str'
<?php die();
/**
* Add you custom models here that you are loading in your controllers
*
* <code>
* $this->site_model->get_records()
* </code>
* Where site_model is the model Class
*
* ---------------------- Models to Load ----------------------
* <examples>
*
str;
$content_ender = <<< 'str'
*/
class my_models
{
}
// End my_models.php
str;
$my_modes_full_path = './my_models.php';
if(!file_exists($my_modes_full_path)){
if($fp=fopen($my_modes_full_path,'w')){
fwrite($fp,$content_header);
} else {
echo '创建文件失败, 请检查是否为权限不足!' . PHP_EOL;
exit();
}
} else {
echo '文件已存在!' . PHP_EOL;
exit();
}
$model_path = APPPATH . 'models/';
$library_path = APPPATH . 'libraries/';
$model_filenames = $this->filename($model_path);
$library_filenames = $this->filename($library_path);
// 去重(也可以不操作去重,不过生成的文件会有红线)
$filenames = array_merge($model_filenames, $library_filenames);
$filenames = array_flip($filenames);
$filenames = array_keys($filenames);
$line = '';
foreach ($filenames as $filename) {
$filename = str_replace(strrchr($filename, "."),"",$filename);
$line .= '* @property ' . $filename .' $'. $filename . "\r\n";
}
fwrite($fp,$line);
fwrite($fp,$content_ender);
fclose($fp);
}
}
- 运行 cli
sudo -u www php index.php test/cli create_my_models
- my_models 文件生成在 index.php同目录下, 替换掉原my_models.php文件即可
注意⚠️
有些model或者library调用时使用的变量可能会有大小写区分,做如下修改
比如: my_models.php 中
/**
* @property Elasticsearch_client $Elasticsearch_client
*/
实际调用的时候使用的是:
$es_result = $this->elasticsearch_client->search('tip', 'tip', $params);
则修改 my_models.php 中对应变量行如下:
/**
* @property Elasticsearch_client $elasticsearch_client
*/