Comments (7)
@cao-minh-duc
Thanks for the example, which illustrates the problem very well. 👍
from laminas-code.
@cao-minh-duc
Have you seen Laminas\Code\Generator\TraitGenerator
:
laminas-code/src/Generator/TraitGenerator.php
Lines 16 to 26 in d5283e9
from laminas-code.
Tks u. But i'm talking about Generate a Class contain Traits. Not Generate a Trait
from laminas-code.
@cao-minh-duc
Same directory:
from laminas-code.
@cao-minh-duc
Same directory:
Yes. I see that and it also in
but it seem not work with method fromReflectionfrom laminas-code.
@cao-minh-duc
First: the documentation is missing for traits – this is a bug.
To understand what your goal is, some example code would be helpful here.
from laminas-code.
@cao-minh-duc
First: the documentation is missing for traits – this is a bug.To understand what your goal is, some example code would be helpful here.
-- \App\Models\User
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
-- Generate logic
$generator = ClassGenerator::fromReflection(
new ClassReflection(\App\Models\User::class)
);
echo $generator->generate();
-- Result
namespace App\Models;
class User extends \Illuminate\Foundation\Auth\User
{
public const CREATED_AT = 'created_at';
public const UPDATED_AT = 'updated_at';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get a new factory instance for the model.
*
* @param mixed $parameters
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public static function factory(... $parameters)
{
$factory =
}
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
//
}
/**
* Get the entity's notifications.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function notifications()
{
return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}
/**
* Get the entity's read notifications.
*
* @return \Illuminate\Database\Query\Builder
*/
public function readNotifications()
{
return $this->notifications()->read();
}
/**
* Get the entity's unread notifications.
*
* @return \Illuminate\Database\Query\Builder
*/
public function unreadNotifications()
{
return $this->notifications()->unread();
}
/**
* Send the given notification.
*
* @param mixed $instance
* @return void
*/
public function notify($instance)
{
app(Dispatcher::class)->send($this, $instance);
}
/**
* Send the given notification immediately.
*
* @param mixed $instance
* @param array|null $channels
* @return void
*/
public function notifyNow($instance, ?array $channels = null)
{
app(Dispatcher::class)->sendNow($this, $instance, $channels);
}
/**
* Get the notification routing information for the given driver.
*
* @param string $driver
* @param \Illuminate\Notifications\Notification|null $notification
* @return mixed
*/
public function routeNotificationFor($driver, $notification = null)
{
if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
return $this->{$method}($notification);
}
switch ($driver) {
case 'database':
return $this->notifications();
case 'mail':
return $this->email;
}
}
}
-- Expect
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
from laminas-code.
Related Issues (20)
- Undefined method FileGenerator::fromReflectedFilePath() HOT 2
- Unable to use with magento 2.4.4 and PHP 8.1 HOT 6
- Generated method declaration return types are not complaint to PSR12 HOT 2
- Dependency Dashboard
- PHP 8.2 compability HOT 19
- Add setAttributes to ClassGenerator HOT 5
- Remove all `@deprecated` code from the component in `5.0.0`
- ClassGenerator syntax error, unexpected token ")" line 1096 HOT 2
- Tag parsers fail parsing typed array definition like array<int> HOT 4
- Implement generateShortOrCompleteClassname method for MethodGenerator and TypeGenerator HOT 4
- Missing line breaks in `DocBlockScanner::getLongDescription` result.
- Space after close parenthesis when return type is presen in the method generator HOT 3
- Issue with extending \ArrayObject HOT 3
- Laminas does not support union types HOT 1
- Promoted properties missing default value
- Promoted properties are losing nullable flag HOT 1
- TypeGenerator fails with type "null" cannot be nullable
- security venerability - see the report HOT 1
- PHP 8.3: New features
- Type "?array|string|null" is a union type, and therefore cannot be also marked nullable with the "?" prefix HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laminas-code.