nxu/php-nano-class-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nxu/php-nano-class-parser

最新稳定版本:v2.0

Composer 安装命令:

composer require nxu/php-nano-class-parser

包简介

A fully opinionated, extremely minimal, and very optimistic package to help add custom code to generated PHP classes.

README 文档

README

tests phpstan Packagist Version (custom server)

This is a very simple package that helps you parse some information about some PHP files.

Stand With Ukraine

Philosophy

The intended purpose of this package is to determine the presence and optionally the vertical position of some statements in a PHP file. This is useful in situations like programmatically adding extra boilerplate to generated files.

Provided analyses:

  • Imports (use <FQN>;)
  • Class definition (class Someting {})
  • Trait uses (use <Trait>;)
  • First function of a class (public static function Something {})

My use case

I am maintaining a CMS that is based on Laravel and Filament. Part of the CMS are commands that generate PHP files (such as Laravel Models and Filament Resources). During generation, my goals are:

  • Use the built-in generators
  • Append some custom PHP boilerplate to it

This package helps me determine where to add custom namespace uses, member properties and member functions as crude strings.

Main features / restrictions

Extremely mimimal
The package only provides what I need, nothing more.
Fully opinionated
The package provides its functionality exactly how I need it.
Very optimistic
This is a very nice way of saying "I don't really care about edge cases".

How?

Install

composer require nxu/php-nano-class-parser --dev

Use

TLDR

$class = PhpClass::parse(
<<<'PHP'
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LaravelModel extends Model
{
    use HasFactory;
}
PHP
);

$imports = $class->analyze()->imports();

$imports->firstLine;
// 5
 
$imports->lastLine; 
// 6

$imports->imports; 
// [
//     'Illuminate\Database\Eloquent\Factories\HasFactory',
//     'Illuminate\Database\Eloquent\Model',
// ]

Available analyses

$class = \Nxu\PhpNanoClassParser\PhpClass::parse('...PHP source code...');

$class->analyze()->classDefinition();
$class->analyze()->classOutline();
$class->analyze()->firstFunction();
$class->analyze()->traits();
$class->analyze()->imports();

License

This package is licensed under the MIT license.

统计信息

  • 总下载量: 5.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-16