feat: add code parse php namespace
This commit is contained in:
parent
4a8e7b7dc8
commit
ba6620f040
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\CommentRequest;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Comment;
|
||||
use Auth;
|
||||
use Validator;
|
||||
|
||||
class CommentController extends Controller
|
||||
{
|
||||
public function create(CommentRequest $request)
|
||||
{
|
||||
$input_comment = $request->input('comment');
|
||||
$post = Post::where('hash', $input_comment['post_hash'])->first();
|
||||
$input_comment['user_id'] = Auth::id();
|
||||
$input_comment['post_id'] = $post->id;
|
||||
|
||||
if (\session('edit_post_id') === $post->id) {
|
||||
Comment::create($input_comment);
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
public function update(CommentRequest $request)
|
||||
{
|
||||
$input_comment = $request->input('comment');
|
||||
$comment_id = (int)$request->comment_id;
|
||||
$post = Post::where('hash', $input_comment['post_hash'])->first();
|
||||
|
||||
if (\session('edit_post_id') === $post->id) {
|
||||
$comment = Auth::user()->comments()->find($comment_id);
|
||||
$comment->fill($input_comment);
|
||||
$comment->save();
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\PostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Models\Category;
|
||||
use Hashids\Hashids;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
const CREATE_POST_FLAG = -1;
|
||||
const PAGINATE_NUMBER = 12;
|
||||
|
||||
public function index()
|
||||
{
|
||||
$posts = Post::with('pictures')->orderBy('created_at', 'asc')->paginate(self::PAGINATE_NUMBER);
|
||||
$categories = Category::orderBy('id')->get();
|
||||
return view('home.index', compact('posts', 'categories'));
|
||||
}
|
||||
|
||||
|
||||
public function edit(Request $request, Post $post = null)
|
||||
{
|
||||
$edit_post_id = self::CREATE_POST_FLAG;
|
||||
if ($post === null) {
|
||||
$post = new Post;
|
||||
} else {
|
||||
$edit_post_id = $post->id;
|
||||
}
|
||||
|
||||
$request->session()->flash('edit_post_id', $edit_post_id);
|
||||
|
||||
$categories = Category::orderBy('id')->get();
|
||||
return view('admin.post.edit', compact('post', 'categories'));
|
||||
}
|
||||
|
||||
public function create(PostRequest $request)
|
||||
{
|
||||
$input_post = $request->input('post');
|
||||
$input_post['published_at'] = date_format(new \DateTime(), 'Y-m-d H:i:s');
|
||||
$input_post['user_id'] = Auth::id();
|
||||
|
||||
if (\session('edit_post_id') === self::CREATE_POST_FLAG) {
|
||||
$hashids = new Hashids(config('APP_KEY'), Post::HASH_LEN);
|
||||
$post = Post::create($input_post);
|
||||
$post->hash = $hashids->encode($post->id);
|
||||
$post->save();
|
||||
$request->session()->flash('edit_post_id', self::CREATE_POST_FLAG);
|
||||
}
|
||||
|
||||
return redirect()->route('admin.index');
|
||||
}
|
||||
|
||||
public function update(PostRequest $request, Post $post)
|
||||
{
|
||||
$input_post = $request->input('post');
|
||||
$input_post['published_at'] = date_format(new \DateTime(), 'Y-m-d H:i:s');
|
||||
$input_post['user_id'] = Auth::id();
|
||||
|
||||
if (\session('edit_post_id') === $post->id) {
|
||||
$post->fill($input_post);
|
||||
$post->save();
|
||||
$request->session()->flash('edit_post_id', self::CREATE_POST_FLAG);
|
||||
}
|
||||
|
||||
return redirect()->route('admin.index');
|
||||
}
|
||||
|
||||
public function show(Request $request, Post $post)
|
||||
{
|
||||
$request->session()->flash('edit_post_id', $post->id);
|
||||
|
||||
return view('home.detail', compact('post'));
|
||||
}
|
||||
|
||||
public function delete(Post $post)
|
||||
{
|
||||
$post and $post->delete();
|
||||
return redirect()->route('admin.index');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use App\Rules\Category;
|
||||
|
||||
|
||||
class PostRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
if ($this->path !== 'post') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// 'post.category_id' => new Category,
|
||||
'post.thumb' => 'between:1,255',
|
||||
'post.title' => 'between:0,255',
|
||||
'post.content' => 'between:0,8191'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
public function posts()
|
||||
{
|
||||
return $this->hasMany('App\Post');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id', 'title', 'thumb', 'content', 'published_at', 'category_id'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('App\User');
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany('App\Comment');
|
||||
}
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo('App\Category');
|
||||
}
|
||||
|
||||
public function pictures()
|
||||
{
|
||||
return $this->hasMany('App\Picture');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Http\Controllers\CommentController;
|
||||
use App\Http\Controllers\PostController;
|
||||
use App\Http\Requests\PostRequest;
|
||||
|
|
@ -40,6 +40,7 @@
|
|||
"lodash": "^4.17.10",
|
||||
"lodash.debounce": "^4.0.8",
|
||||
"madge": "^3.4.4",
|
||||
"php-parser": "^3.0.2",
|
||||
"portscanner": "^2.2.0",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
|
|
|
|||
|
|
@ -29,14 +29,19 @@ const DependenciesTree = props => {
|
|||
<React.Fragment>
|
||||
{selectedNodeDependencies &&
|
||||
[selectedNodeDependencies].map(({ moduleName, importedModuleNames }) => {
|
||||
const moduleNode = filesLayoutMap[moduleName];
|
||||
const moduleNode = filesLayoutMap[selectedNode.path];
|
||||
if (!moduleNode) return null;
|
||||
|
||||
const [mX, mY] = [moduleNode.y, moduleNode.x];
|
||||
const targetPosition = shiftToCenterPoint(mX, mY);
|
||||
|
||||
const importedNodes = importedModuleNames
|
||||
.map(name => filesLayoutMap[name])
|
||||
.map(moduleName => {
|
||||
const key = Object.keys(selectedNode.dependencies).find(key => {
|
||||
return selectedNode.dependencies[key].moduleName === moduleName
|
||||
})
|
||||
return filesLayoutMap[key]
|
||||
})
|
||||
.filter(node => !!node);
|
||||
|
||||
const edges = [];
|
||||
|
|
|
|||
|
|
@ -1,7 +1,76 @@
|
|||
const defaultDependencies = require('../default/dependencies');
|
||||
const readdirRecursive = require('fs-readdir-recursive')
|
||||
const file = require('../../../utils/file')
|
||||
|
||||
const engine = require('php-parser');
|
||||
const path = require('path');
|
||||
|
||||
// initialize a new parser instance
|
||||
var parser = new engine({
|
||||
// some options :
|
||||
parser: {
|
||||
extractDoc: true,
|
||||
php7: true
|
||||
},
|
||||
ast: {
|
||||
withPositions: true
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const getDependencies = (entryPoint, projectDir) => {
|
||||
// const rootPath = path.resolve()
|
||||
const separator = path.sep;
|
||||
|
||||
const phpFileNamespaces = {};
|
||||
|
||||
const tasks = readdirRecursive(projectDir).map(async f => {
|
||||
const filePath = `${projectDir}${separator}${f.replace(/\//g, separator)}`
|
||||
|
||||
phpFile = await file.read(filePath, 'utf8')
|
||||
|
||||
const parsed = parser.parseCode(phpFile);
|
||||
|
||||
parsed.children?.forEach(parsedChild => {
|
||||
const namespace = {
|
||||
moduleName: parsedChild.name,
|
||||
importedModuleNames: []
|
||||
}
|
||||
parsedChild.children?.forEach(c => {
|
||||
switch (c.kind) {
|
||||
case 'usegroup':
|
||||
c.items.forEach(item => {
|
||||
namespace.importedModuleNames.push(item.name)
|
||||
})
|
||||
break
|
||||
case 'class':
|
||||
namespace.moduleName = `${parsedChild.name}\\${c.name.name}`
|
||||
}
|
||||
})
|
||||
|
||||
phpFileNamespaces[filePath] = namespace
|
||||
})
|
||||
})
|
||||
return Promise.all(tasks).then(() => {
|
||||
const dependencies = {}
|
||||
dependencies[entryPoint] = phpFileNamespaces[entryPoint]
|
||||
|
||||
phpFileNamespaces[entryPoint]?.importedModuleNames.forEach((moduleName) => {
|
||||
Object.keys(phpFileNamespaces).forEach(filePath => {
|
||||
if (phpFileNamespaces[filePath].moduleName !== moduleName) {
|
||||
return
|
||||
}
|
||||
|
||||
dependencies[filePath] = phpFileNamespaces[filePath]
|
||||
})
|
||||
})
|
||||
|
||||
return dependencies
|
||||
})
|
||||
};
|
||||
|
||||
// replace with own implementation if needed
|
||||
module.exports = {
|
||||
getImports: defaultDependencies.getImports,
|
||||
getDependencies: defaultDependencies.getDependencies
|
||||
getDependencies: getDependencies
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue