Comments (6)
Short answer is no.
The Default PhpParser printer "is not very suitable for refactoring code". So I extended it in PSR2PrettyPrinter and it is slightly better, but this is one of the unwanted behaviors.
from archetype.
Hi, thanks for question :)
The property
method accepts either a primitive value, or an instance of a PhpParser Node instance. So we should be able to do something like this.
use PhpParser\BuilderFactory;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\String_;
$factory = new PhpParser\BuilderFactory();
$scriptsValueNode = $factory->concat(
new Dir,
new String_('/../dist/js/addon.js')
);
LaravelFile::user()
->add()->protected()->property('scripts', $scriptsValueNode)
->preview()
In general I would go about it like this:
- put an example of what you would like to write in a file
- load it and review the AST using:
LaravelFile::user()->ast()
- see what class name your example has. In your first example that would be
PhpParser\Node\Expr\ClassConstFetch
- Use
PhpParser\BuilderFactory
to construct that node
from archetype.
Would love to see how you are using it when you're finished. Just a heds up, I have not looked into how PHP8 affects this package. Seems to work fine but it does not yet support constructor promotion etc. Maybe that is not to big of a problem.
from archetype.
Thanks for the reply! I'm back to trying to figure this out again and wondering how I might be able to pass a value like Fieldtypes\YourFieldtype:class
into a class variable since it's not a string or array?
from archetype.
Ah, I think i just got it!
$fieldtypeClassValue = $factory->classConstFetch('Fieldtypes\KnightRider', 'class');
PHPFile::load('addons/rad-city/knight-writer/src/ServiceProvider.php')
->add()->protected()->property('scripts', $scriptsValue)
->add()->protected()->property('fieldtypes', $fieldtypeClassValue)
->save();
Which gave me...
class ServiceProvider extends AddonServiceProvider
{
protected $fieldtypes = [Fieldtypes\EmptyLacroixCan::class];
protected $scripts = [__DIR__ . '/../dist/js/addon.js'];
}
Is there any way to force some formatting? Like each value on a new line, and empty lines between classes?
from archetype.
Every consider tapping into something like https://github.com/FriendsOfPHP/PHP-CS-Fixer?
from archetype.
Related Issues (15)
- Reading grouped imports HOT 1
- Getter & Setter for class constants HOT 1
- Composer 2 PSR-4 Autoloading Conflict HOT 1
- Using testbench package for testing HOT 3
- PHP 8 Support HOT 7
- Laravel 9 Support HOT 1
- Add traitUse() HOT 5
- Rewrite StmtInserter
- How to add methods? HOT 1
- Project fails to load files HOT 6
- Add return parent::method() to class body HOT 1
- [Question] How can I add a new key/value inside an array of a laravel config file ? HOT 2
- Document how to use in a Symfony project HOT 3
- running phpunit tests HOT 1
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 archetype.