Spatie Invade

repository·main·Indexed 43 days ago

https://github.com/spatie/invade

A PHP package that provides the invade function and Invader/StaticInvader classes to bypass visibility constraints, allowing developers to read and write private properties and call private methods on both object instances and classes.

Tokens
2.3K
Snippets
9
Records
11
Agent score
54%

What's inside Invade

  1. Usage rights for the Invade logo

    main

    The Invade logo is subject to specific licensing conditions.

    Permitted Uses

    You may use the logo in the following cases:

    • Marketing materials for technical events (meetups, hackathons, conferences, workshops) related to Laravel.
    • Open source projects related to Laravel.
    • Technical articles, videos, books, or papers for educational purposes.
    • To illustrate a commercial product.

    Prohibited Uses

    You may NOT use the logo in the following cases without prior written consent:

    • Using the logo in a commercial product for purposes other than illustrating its integration.
    • Selling physical products that use the Invade logo or its variants (e.g., t-shirts).
  2. Access and modify private properties of an object

    main

    The invade function allows you to bypass visibility constraints to read or write private properties on an object instance using property access syntax.

    class MyClass
    {
        private string $privateProperty = 'private value';
    }
    
    $myClass = new MyClass();
    
    // Read a private property
    invade($myClass)->privateProperty; // returns 'private value'
    
    // Write to a private property
    invade($myClass)->privateProperty = 'changed value';
    invade($myClass)->privateProperty; // returns 'changed value'
  3. Call private static methods

    main

    To call private static methods, pass the class name to invade(), specify the method name using method(), and then execute it with call(), passing any required arguments.

    class MyClass
    {
        private static function privateStaticMethod(string $string, int $int): string
        {
            return 'private return value ' . $string . ' ' . $int;
        }
    }
    
    // Call a private static method with arguments
    invade(MyClass::class)
        ->method('privateStaticMethod')
        ->call('foo', 123);
    
    // returns 'private return value foo 123'
  4. Call private methods on an object

    main

    You can invoke private methods on an object instance by calling them directly through the invade wrapper.

    class MyClass
    {
        private function privateMethod(): string
        {
            return 'private return value';
        }
    }
    
    $myClass = new MyClass();
    
    // Call a private method
    invade($myClass)->privateMethod(); // returns 'private return value'
  5. Access and modify private static properties

    main

    To interact with private static properties, pass the class name (as a string) to invade() instead of an object instance. Use the get() and set() methods to interact with these properties.

    class MyClass
    {
        private static string $privateStaticProperty = 'privateValue';
    }
    
    // Get a private static property
    invade(MyClass::class)->get('privateStaticProperty'); // returns 'privateValue'
    
    // Set a private static property
    invade(MyClass::class)->set('privateStaticProperty', 'changedValue');
    
    // Verify the change
    invade(MyClass::class)->get('privateStaticProperty'); // returns 'changedValue'
  6. Invade brand colors and fonts

    main

    The Invade brand uses the following design assets:

    Fonts

    Color Palette

    | Color | Hex | rgb() |
    |--- |--- |--- |
    | ![100](/art/palette/100.png) | `#E8F1F4` | `rgb(232,241,244)` |
    | ![200](/art/palette/200.png) | `#C6DDE4` | `rgb(198,221,228)` |
    | ![300](/art/palette/300.png) | `#A3C8D4` | `rgb(163,200,212)` |
    | ![400](/art/palette/400.png) | `#5E9EB3` | `rgb(94,158,179)` |
    | ![500](/art/palette/500.png) | `#197593` | `rgb(25,117,147)` |
    | ![600](/art/palette/600.png) | `#176984` | `rgb(23,105,132)` |
    | ![700](/art/palette/700.png) | `#0F4658` | `rgb(15,70,88)` |
    | ![800](/art/palette/800.png) | `#0B3542` | `rgb(11,53,66)` |
    | ![900](/art/palette/900.png) | `#08232C` | `rgb(8,35,44)` |
  7. Call private static methods using method() and call()

    main

    To invoke a private static method, you must first chain the method() method to specify the method name, and then call call() to execute it. If you attempt to call call() without first specifying a method via method(), an Exception will be thrown.

    Example usage: invadeStatic(Foo::class)->method('bar')->call()

    use Spatie\Invade\StaticInvader;
    
    // Correct way to call a private static method
    $result = (new StaticInvader(Foo::class))->method('privateMethodName')->call($param1, $param2);
  8. Access private static properties and methods with StaticInvader

    main

    The StaticInvader class allows you to bypass visibility constraints to access private or protected static properties and methods of a given class.

    • Use get(string $name) to retrieve the value of a private static property.
    • Use set(string $name, mixed $value) to update a private static property.
    • Use method(string $name) to specify which static method you want to invoke, followed by call(...$params) to execute it with the provided arguments.
    use Spatie\Invade\StaticInvader;
    
    // Accessing a private static property
    $value = (new StaticInvader(Foo::class))->get('privateStaticProperty');
    
    // Setting a private static property
    (new StaticInvader(Foo::class))->set('privateStaticProperty', 'new value');
    
    // Calling a private static method
    $result = (new StaticInvader(Foo::class))->method('privateStaticMethod')->call('arg1', 'arg2');
  9. Use the Invader class to access private properties and methods

    main

    The Invader class allows you to bypass visibility constraints (private and protected) on an object's properties and methods. By wrapping an object in an Invader instance, you can interact with its internal state and logic as if they were public.

    • Accessing Properties: Use standard property access syntax. The __get magic method executes a closure bound to the original object to retrieve the value.
    • Setting Properties: Use standard assignment syntax. The __set magic method executes a closure bound to the original object to update the value.
    • Calling Methods: Use standard method call syntax. The __call magic method executes a closure bound to the original object, passing any provided arguments through to the target method.
    use Spatie\Invade\Invader;
    
    class Secret
    {
        private string $secret = 'shhh';
    
        private function reveal(string $prefix): string
        {
            return $prefix . $this->secret;
        }
    }
    
    $secret = new Secret();
    $invader = new Invader($secret);
    
    // Access private property
    echo $invader->secret; // "shhh"
    
    // Call private method
    echo $invader->reveal('The secret is: '); // "The secret is: shhh"