├── README.md ├── composer.json └── src ├── EloquentTentacle.php ├── Parasite.php └── StaticParasite.php /README.md: -------------------------------------------------------------------------------- 1 | # tentacles 2 | *Monkey-patching for eloquent models* 3 | 4 | Composer 5 | ``` 6 | "greabock/tentacles": "dev-master" 7 | ``` 8 | 9 | user-model... 10 | ```php 11 | hasMany(Article::class); 48 | }); 49 | 50 | 51 | User::addExternalMethod('getFullnameAttribute', function() 52 | { 53 | return $this->first_name . ' ' . $this->last_name; 54 | }); 55 | } 56 | 57 | } 58 | 59 | ``` 60 | 61 | Now we can do this: 62 | 63 | ``` 64 | $user = User::with('articles')->first(); 65 | 66 | $fullname = $user->fullname; 67 | ``` 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "greabock/tentacles", 3 | "description": "Da epic tentacles for Eloquent", 4 | "keywords": [ 5 | "laravel", 6 | "eloquent", 7 | "trait", 8 | "model" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Greabock", 14 | "email": "greabock@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.5.0", 19 | "illuminate/support" : "5.*", 20 | "illuminate/database" : "5.*" 21 | }, 22 | 23 | "autoload": { 24 | "psr-4": { 25 | "Greabock\\Tentacles\\": "src/" 26 | } 27 | }, 28 | 29 | "extra": { 30 | "branch-alias": { 31 | "dev-master": "1.0.2-dev" 32 | } 33 | }, 34 | 35 | "minimum-stability": "dev" 36 | } 37 | -------------------------------------------------------------------------------- /src/EloquentTentacle.php: -------------------------------------------------------------------------------- 1 | relationLoaded($key)) { 55 | return $this->relations[$key]; 56 | } 57 | 58 | if (isset(static::$externalMethods[$key])) { 59 | return $this->getRelationshipFromMethod($key); 60 | } 61 | 62 | if (method_exists($this, $key)) { 63 | return $this->getRelationshipFromMethod($key); 64 | } 65 | } 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/Parasite.php: -------------------------------------------------------------------------------- 1 | __callAfter($method, $parameters); 36 | } 37 | 38 | // Keep ownder's ancestor functional 39 | if (method_exists(parent::class, '__call')) { 40 | return parent::__call($method, $parameters); 41 | } 42 | 43 | throw new BadMethodCallException('Method ' . static::class . '::' . $method . '() not found'); 44 | } 45 | 46 | /** 47 | * @param string $name 48 | * @param Closure $method 49 | * @return void 50 | */ 51 | public static function addExternalMethod($name, Closure $method) 52 | { 53 | static::$externalMethods[$name] = $method; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/StaticParasite.php: -------------------------------------------------------------------------------- 1 |