file = $file; $this->space = $space; $this->header = ' '备注信息'), ... * @param array $data 对应值数组 array('变量名称' => '变量值'), ... 为空时直接生成$var * @return int */ public function to_require_one($var, $data = []) { $body = $this->header.'return ['.PHP_EOL.PHP_EOL; if ($data) { foreach ($var as $name => $note) { if (is_array($data[$name])) { continue; } $name = $this->_safe_replace($name); $body.= ' \''.$name.'\''.$this->_space($name).'=> '.$this->_format_value($data[$name]).', //'.$note.PHP_EOL; } } elseif ($var) { foreach ($var as $name => $val) { if (is_array($val)) { continue; } $name = $this->_safe_replace($name); $body.= ' \''.$name.'\''.$this->_space($name).'=> '.$this->_format_value($val).','.PHP_EOL; } } $body.= PHP_EOL.'];'; !is_dir(dirname($this->file)) && dr_mkdirs(dirname($this->file)); // 重置Zend OPcache function_exists('opcache_reset') && opcache_reset(); return @file_put_contents($this->file, $body, LOCK_EX); } /** * 生成require N维数组文件 * * @param array data * @return int */ public function to_require($data) { if (!is_array($data)) { $data = []; } $body = $this->header.'return '; $body .= str_replace(array(' ', ' '), array(' ', ' '), var_export($data, TRUE)); $body .= ';'; !is_dir(dirname($this->file)) && dr_mkdirs(dirname($this->file)); // 重置Zend OPcache function_exists('opcache_reset') && opcache_reset(); return @file_put_contents($this->file, $body, LOCK_EX); } /** * 生成require N维数组文件 * * @param array data * @return int */ public function to_require_array($value) { if (!$value) { return NULL; } $body = $this->header.'return ['.PHP_EOL.PHP_EOL; foreach ($value as $id => $data) { $body.= ' '.$id .' => ['.PHP_EOL.PHP_EOL; foreach ($data as $name => $val) { if (is_array($data[$name])) { continue; } $name = $this->_safe_replace($name); $body.= ' \''.$name.'\''.$this->_space($name).'=> '.$this->_format_value($data[$name]).','.PHP_EOL; } $body.= PHP_EOL.' ],'.PHP_EOL.PHP_EOL; } $body.= PHP_EOL.'];'; !is_dir(dirname($this->file)) && dr_mkdirs(dirname($this->file)); // 重置Zend OPcache function_exists('opcache_reset') && opcache_reset(); return @file_put_contents($this->file, $body, LOCK_EX); } /** * 补空格 */ private function _space($name) { $len = dr_strlen($name) + 2; $cha = $this->space - $len; $str = ''; for ($i = 0; $i < $cha; $i ++) $str .= ' '; return $str; } /** * 格式化值 */ private function _format_value($value) { return is_numeric($value) && strlen($value) <= 10 ? $value : '\''.str_replace(['\'', '\\'], '', (string)$value).'\''; } /** * 安全替换 */ private function _safe_replace($name) { return str_replace( ['..', '\\', '<', '>', "{", '}', ';', '[', ']', '\'', '"', '*', '?'], '', (string)$name ); } }