<commit_before><?php
namespace ebi;
/**
 * 開発支援ツール
 * @author tokushima
 *
 */
class Dt{
	use \ebi\FlowPlugin;
	
	private $entry;
	private $entry_name;
	private $self_class;
	
	public function __construct($entryfile=null){
		if(empty($entryfile)){
			$this->self_class = __CLASS__;
			$trace = debug_backtrace(false);
			krsort($trace);
			
			foreach($trace as $t){
				if(isset($t['class']) && $t['class'] == 'ebi\Flow'){
					$this->entry = $t['file'];
					$this->entry_name = basename($this->entry,'.php');
					break;
				}
			}
		}else{
			$entryfile = realpath($entryfile);
			$this->entry = $entryfile;
			$this->entry_name = basename($this->entry,'.php');
		}
	}
	public function get_flow_plugins(){
		return [
			\ebi\flow\plugin\HtmlMinifier::class,
		];
	}
	public function get_after_vars(){
		$vars = [
			'f'=>new \ebi\Dt\Helper(),
			'appmode'=>constant('APPMODE'),
			'has_test'=>is_dir(self::test_path()),
		];
		return $vars;
	}
	/**
	 * @automap
	 */
	public function index(){
		$flow_output_maps = [];
		
		$map = \ebi\Flow::get_map($this->entry);
		$patterns = $map['patterns'];
		unset($map['patterns']);

		$req = new \ebi\Request();
		$target_version = $req->in_vars('version');
		$file_version = date('Ymd',filemtime($this->entry));
		$version_list = [];
		
		foreach($patterns as $k => $m){
			foreach([
				'deprecated'=>false,
				'mode'=>null,
				'summary'=>null,
				'template'=>null,
				'version'=>null,
				'error'=>null,
				'login'=>false,
			] as $i => $d){
				if(!isset($m[$i])){
					$m[$i] = $d;
				}
			}
			if(isset($m['action']) && is_string($m['action'])){
				list($m['class'],$m['method']) = explode('::',$m['action']);
			}
			if(!isset($m['class']) || $m['class'] != $this->self_class){
				try{
					$m['error'] = null;
					$m['url'] = $k;
					
					if(isset($m['method'])){
						$info = \ebi\Dt\Man::method_info($m['class'],$m['method']);
						
						if(!isset($m['version'])){
							$m['version'] = $info->version();
						}
						if(empty($m['summary'])){
							list($summary) = explode(PHP_EOL,$info->document());
							$m['summary'] = empty($summary) ? null : $summary;
						}
						if($m['deprecated'] || $info->opt('deprecated')){
							$m['deprecated'] = true;
						}
						if($m['deprecated'] || !empty($info->opt('first_depricated_date'))){
							$m['first_depricated_date'] = $info->opt('first_depricated_date', time());
						}
					}
				}catch(\Exception $e){
					$m['error'] = $e->getMessage();
				}
				foreach($m as $k => $v){
					if(is_array($v) && isset($map[$k]) && !empty($map[$k])){
						$m[$k] = array_merge($map[$k],$v);
					}else{
						if(!isset($v) && isset($map[$k])){
							$m[$k] = $map[$k];
						}
					}
				}
				
				if(!isset($m['version'])){
					$m['version'] = $file_version;
				}
				$version_list[$m['version']] = $m['version'];
				
				if(empty($target_version) || $m['version'] == $target_version){
					$flow_output_maps[$m['name']] = $m;
				}
			}
		}
		krsort($version_list);
		
		$entry_desc = (preg_match('/\/\*\*.+?\*\//s',\ebi\Util::file_read($this->entry),$m)) ?
			trim(preg_replace("/^[\s]*\*[\s]{0,1}/m",'',str_replace(['/'.'**','*'.'/'],'',$m[0]))) :
			'';
		
		return [
			'map_list'=>$flow_output_maps,
			'description'=>$entry_desc,
			'version'=>(empty($target_version) ? null : $target_version),
			'version_list'=>$version_list,
		];
	}	
	/**
	 * アクションのドキュメント
	 * @param string $name
	 * @context \ebi\man\DocInfo $method
	 * @automap
	 */
	public function index_action_doc($name){
		$map = \ebi\Flow::get_map($this->entry);
		
		foreach($map['patterns'] as $m){
			if($m['name'] == $name){
				if(isset($m['action'])){
					if($m['action'] instanceof \Closure){
						$info = \ebi\Dt\Man::closure_info($m['action']);
					}else{
						list($m['class'],$m['method']) = explode('::',$m['action']);
						list(,$user_model) = $this->get_login_annotation($m['class'],$m['method']);
						
						$info = \ebi\Dt\Man::method_info($m['class'],$m['method'],true,true);
						$info->set_opt('user_model',$user_model);
						foreach(['get_after_vars','get_after_vars_request'] as $mn){
							try{
								$ex_info = \ebi\Dt\Man::method_info($m['class'],$mn,true,true);
								
								foreach(['requests','contexts'] as $k){
									$info->set_opt($k,array_merge($ex_info->opt($k),$info->opt($k)));
								}
							}catch(\ReflectionException $e){
							}
						}
						
						// ログイン プラグイン情報をマージ
						foreach($info->opt('call_plugins') as $plugin){
							if($plugin->name() == 'login_condition'){
								foreach(array_merge(($m['plugins'] ?? []),($map['plugins'] ?? [])) as $map_plugin){
									$plugin_class = \ebi\Util::get_class_name($map_plugin);
									$ref = new \ReflectionClass($plugin_class);
									$document = trim(preg_replace('/\n*@.+/','',PHP_EOL.\ebi\Dt\Man::trim_doc($ref->getDocComment())));
									$info->document(trim($info->document().PHP_EOL.$document));
									
									foreach($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $m){
										if($m->getName() == 'login_condition' || $m->getName() == 'get_after_vars_login'){
											$login_method = \ebi\Dt\Man::method_info($plugin_class,$m->getName());
											
											if($login_method->has_opt('http_method')){
												$info->set_opt('http_method',$login_method->opt('http_method'));
											}
											foreach(['requests','contexts'] as $k){
												$info->set_opt($k,array_merge($login_method->opt($k),$info->opt($k)));
											}
										}<commit_msg>bugfix<commit_after><?php
namespace ebi;
/**
 * 開発支援ツール
 * @author tokushima
 *
 */
class Dt{
	use \ebi\FlowPlugin;
	
	private $entry;
	private $entry_name;
	private $self_class;
	
	public function __construct($entryfile=null){
		if(empty($entryfile)){
			$this->self_class = __CLASS__;
			$trace = debug_backtrace(false);
			krsort($trace);
			
			foreach($trace as $t){
				if(isset($t['class']) && $t['class'] == 'ebi\Flow'){
					$this->entry = $t['file'];
					$this->entry_name = basename($this->entry,'.php');
					break;
				}
			}
		}else{
			$entryfile = realpath($entryfile);
			$this->entry = $entryfile;
			$this->entry_name = basename($this->entry,'.php');
		}
	}
	public function get_flow_plugins(){
		return [
			\ebi\flow\plugin\HtmlMinifier::class,
		];
	}
	public function get_after_vars(){
		$vars = [
			'f'=>new \ebi\Dt\Helper(),
			'appmode'=>constant('APPMODE'),
			'has_test'=>is_dir(self::test_path()),
		];
		return $vars;
	}
	/**
	 * @automap
	 */
	public function index(){
		$flow_output_maps = [];
		
		$map = \ebi\Flow::get_map($this->entry);
		$patterns = $map['patterns'];
		unset($map['patterns']);

		$req = new \ebi\Request();
		$target_version = $req->in_vars('version');
		$file_version = date('Ymd',filemtime($this->entry));
		$version_list = [];
		
		foreach($patterns as $k => $m){
			foreach([
				'deprecated'=>false,
				'mode'=>null,
				'summary'=>null,
				'template'=>null,
				'version'=>null,
				'error'=>null,
				'login'=>false,
			] as $i => $d){
				if(!isset($m[$i])){
					$m[$i] = $d;
				}
			}
			if(isset($m['action']) && is_string($m['action'])){
				list($m['class'],$m['method']) = explode('::',$m['action']);
			}
			if(!isset($m['class']) || $m['class'] != $this->self_class){
				try{
					$m['error'] = null;
					$m['url'] = $k;
					
					if(isset($m['method'])){
						$info = \ebi\Dt\Man::method_info($m['class'],$m['method']);
						
						if(!isset($m['version'])){
							$m['version'] = $info->version();
						}
						if(empty($m['summary'])){
							list($summary) = explode(PHP_EOL,$info->document());
							$m['summary'] = empty($summary) ? null : $summary;
						}
						if($m['deprecated'] || $info->opt('deprecated')){
							$m['deprecated'] = true;
						}
						if($m['deprecated'] || !empty($info->opt('first_depricated_date'))){
							$m['first_depricated_date'] = $info->opt('first_depricated_date', time());
						}
					}
				}catch(\Exception $e){
					$m['error'] = $e->getMessage();
				}
				foreach($m as $k => $v){
					if(is_array($v) && isset($map[$k]) && !empty($map[$k])){
						$m[$k] = array_merge($map[$k],$v);
					}else{
						if(!isset($v) && isset($map[$k])){
							$m[$k] = $map[$k];
						}
					}
				}
				
				if(!isset($m['version'])){
					$m['version'] = $file_version;
				}
				$version_list[$m['version']] = $m['version'];
				
				if(empty($target_version) || $m['version'] == $target_version){
					$flow_output_maps[$m['name']] = $m;
				}
			}
		}
		krsort($version_list);
		
		$entry_desc = (preg_match('/\/\*\*.+?\*\//s',\ebi\Util::file_read($this->entry),$m)) ?
			trim(preg_replace("/^[\s]*\*[\s]{0,1}/m",'',str_replace(['/'.'**','*'.'/'],'',$m[0]))) :
			'';
		
		return [
			'map_list'=>$flow_output_maps,
			'description'=>$entry_desc,
			'version'=>(empty($target_version) ? null : $target_version),
			'version_list'=>$version_list,
		];
	}	
	/**
	 * アクションのドキュメント
	 * @param string $name
	 * @context \ebi\man\DocInfo $method
	 * @automap
	 */
	public function index_action_doc($name){
		$map = \ebi\Flow::get_map($this->entry);
		
		foreach($map['patterns'] as $m){
			if($m['name'] == $name){
				if(isset($m['action'])){
					if($m['action'] instanceof \Closure){
						$info = \ebi\Dt\Man::closure_info($m['action']);
					}else{
						list($m['class'],$m['method']) = explode('::',$m['action']);
						list(,$user_model) = $this->get_login_annotation($m['class'],$m['method']);
						
						$info = \ebi\Dt\Man::method_info($m['class'],$m['method'],true,true);
						$info->set_opt('user_model',$user_model);
						foreach(['get_after_vars','get_after_vars_request'] as $mn){
							try{
								$ex_info = \ebi\Dt\Man::method_info($m['class'],$mn,true,true);
								
								foreach(['requests','contexts'] as $k){
									$info->set_opt($k,array_merge($ex_info->opt($k),$info->opt($k)));
								}
							}catch(\ReflectionException $e){
							}
						}
						
						// ログイン プラグイン情報をマージ
						foreach($info->opt('call_plugins') as $plugin){
							if($plugin->name() == 'login_condition'){
								foreach(array_merge(($m['plugins'] ?? []),($map['plugins'] ?? [])) as $map_plugin){
									$plugin_class = \ebi\Util::get_class_name($map_plugin);
									$ref = new \ReflectionClass($plugin_class);
									$document = trim(preg_replace('/\n*@.+/','',PHP_EOL.\ebi\Dt\Man::trim_doc($ref->getDocComment())));
									$info->document(trim($info->document().PHP_EOL.$document));
									
									foreach($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $method){
										if($method->getName() == 'login_condition' || $method->getName() == 'get_after_vars_login'){
											$login_method = \ebi\Dt\Man::method_info($plugin_class,$method->getName());
											
											if($login_method->has_opt('http_method')){
												$info->set_opt('http_method',$login_method->opt('http_method'));
											}
											foreach(['requests','contexts'] as $k){
												$info->set_opt($k,array_merge($login_method->opt($k),$info->opt($k)));
											}
										}
{'fraction_non_alphanumeric': 0.2451202905129369, 'fraction_numerical': 0.0005447117566954153, 'mean_word_length': 3.2516402933230415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1918519', 'n_tokens_mistral': 5304, 'n_tokens_neox': 4058, 'n_words': 490}