Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Code Laravel Package

laminas/laminas-code

Generate, analyze, and manipulate PHP code with Laminas Code. Provides reflection utilities, code generators, docblock parsing, and class/file generation helpers for building frameworks, tooling, and meta-programming workflows.

View on GitHub
Deep Wiki
Context7

Introduction

Laminas\Code\Generator provides facilities to generate arbitrary code using an object-oriented interface, both to create new code as well as to update existing code. While the current implementation is limited to generating PHP code, you can easily extend the base class in order to provide code generation for other tasks: JavaScript, configuration files, apache vhosts, etc.

Theory of Operation

In the most typical use case, you will simply instantiate a code generator class and either pass it the appropriate configuration or configure it after instantiation. To generate the code, you will simply echo the object or call its generate() method.

// Passing configuration to the constructor:
$file = new Laminas\Code\Generator\FileGenerator(array(
    'classes' => array(
        new Laminas\Code\Generator\ClassGenerator(
            'World',  // name
            null,     // namespace
            null,     // flags
            null,     // extends
            array(),  // interfaces
            array(),  // properties
            array(
                new Laminas\Code\Generator\MethodGenerator(
                    'hello',                  // name
                    array(),                  // parameters
                    'public',                 // visibility
                    'echo \'Hello world!\';'  // body
                ),
            )
        ),
    ),
));

// Render the generated file
echo $file->generate();

// or write it to a file:
file_put_contents('World.php', $file->generate());

// OR

// Configuring after instantiation
$method = new Laminas\Code\Generator\MethodGenerator();
$method->setName('hello')
       ->setBody('echo \'Hello world!\';');

$class = new Laminas\Code\Generator\ClassGenerator();
$class->setName('World')
      ->addMethodFromGenerator($method);

$file = new Laminas\Code\Generator\FileGenerator();
$file->setClass($class);

// Render the generated file
echo $file->generate();

// or write it to a file:
file_put_contents('World.php', $file->generate());

Both of the above samples will render the same result:

<?php

class World
{

    public function hello()
    {
        echo 'Hello world!';
    }

}

Another common use case is to update existing code -- for instance, to add a method to a class. In such a case, you must first inspect the existing code using reflection, and then add your new method. Laminas\Code\Generator makes this trivially simple, by leveraging Laminas\Code\Reflection.

As an example, let's say we've saved the above to the file World.php, and have already included it. We could then do the following:

$class = Laminas\Code\Generator\ClassGenerator::fromReflection(
    new Laminas\Code\Reflection\ClassReflection('World')
);

$method = new Laminas\Code\Generator\MethodGenerator();
$method->setName('mrMcFeeley')
       ->setBody('echo \'Hello, Mr. McFeeley!\';');
$class->addMethodFromGenerator($method);

$file = new Laminas\Code\Generator\FileGenerator();
$file->setClass($class);

// Render the generated file
echo $file->generate();

// Or, better yet, write it back to the original file:
file_put_contents('World.php', $file->generate());

The resulting class file will now look like this:

<?php

class World
{

    public function hello()
    {
        echo 'Hello world!';
    }

    public function mrMcFeeley()
    {
        echo 'Hellow Mr. McFeeley!';
    }

}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport