{"id":19324957,"url":"https://github.com/spatie/laravel-tinker-tools","last_synced_at":"2025-04-06T02:12:43.166Z","repository":{"id":52794276,"uuid":"91980495","full_name":"spatie/laravel-tinker-tools","owner":"spatie","description":"Use short class names in an Artisan tinker session","archived":false,"fork":false,"pushed_at":"2022-03-21T13:00:22.000Z","size":41,"stargazers_count":138,"open_issues_count":0,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T01:12:28.505Z","etag":null,"topics":["composer","devtools","laravel","php"],"latest_commit_sha":null,"homepage":"https://murze.be/2017/05/package-enable-short-class-names-artisan-tinker-session/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-21T19:00:37.000Z","updated_at":"2024-01-24T13:26:24.000Z","dependencies_parsed_at":"2022-08-24T14:51:18.762Z","dependency_job_id":null,"html_url":"https://github.com/spatie/laravel-tinker-tools","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-tinker-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-tinker-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-tinker-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-tinker-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-tinker-tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423516,"owners_count":20936626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","devtools","laravel","php"],"created_at":"2024-11-10T02:07:56.938Z","updated_at":"2025-04-06T02:12:43.147Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1\" /\u003e](https://supportukrainenow.org)\n\n**The functionality of this package is built into Laravel 5.5 and above, only install this in older Laravel versions**\n\n# Use short class names in an Artisan Tinker session\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-tinker-tools.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-tinker-tools)\n[![Build Status](https://img.shields.io/travis/spatie/laravel-tinker-tools/master.svg?style=flat-square)](https://travis-ci.org/spatie/laravel-tinker-tools)\n[![StyleCI](https://styleci.io/repos/91980495/shield?branch=master)](https://styleci.io/repos/91980495)\n[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-tinker-tools.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-tinker-tools)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-tinker-tools.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-tinker-tools)\n\nWhen using Artisan's Tinker command it can be quite bothersome having to type the fully qualified classname to do something simple.\n\n```php\n\\App\\Models\\NewsItem::first();\n```\n\nThis package contains a class that, when fully installed lets you use the short class names:\n\n```php\nNewsItem::first();\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-tinker-tools.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-tinker-tools)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nFirst install the package via Composer:\n\n``` bash\ncomposer require spatie/laravel-tinker-tools\n```\n\nNext, create a file named `.psysh.php` in the root of your Laravel app with this content:\n\n```php\n\u003c?php\n\n\\Spatie\\TinkerTools\\ShortClassNames::register();\n```\n\nFinally, dump the optimized version of the autoloader so `autoload_classmap.php` gets created:\n\n```bash\ncomposer dump-autoload -o\n```\n\n## Usage\n\nOpen up a Tinker session with:\n\n```bash\nphp artisan tinker\n```\n\nInside that Tinker session you can now use short class names:\n\n```php\nNewsItem::first();\n```\n\n## A peek behind the curtains\n\nWhen you use a class that hasn't been loaded in yet, PHP will call the registered autoloader functions. Such autoloader functions are responsible for loading up the requested class. In a typical project Composer will register an autoloader function that can `include` the file where the class is stored in.\n\nComposer has a few ways to locate the right files. In most cases it will convert the fully qualified class name to a path. For example, when using a class `App\\Models\\NewsItem` Composer will load the file in `app/Models/NewsItem.php`. It's a bit more complicated behind the scenes but that's the gist of it. To make the process of finding a class fast, Composer caches all the fully qualified classnames and their paths in the generated `autoload_classmap.php`, which can be found in `vendor/composer`. \n\nNow, to make this package work, `\\Spatie\\TinkerTools\\ShortClassNames` will read Composer's `autoload_classmap.php` and [convert the fully qualified class names to short class names](https://github.com/spatie/laravel-tinker-tools/blob/d3a3287/src/ShortClassNames.php#L23). The result is a collection that's being kept in [the `$classes` property](https://github.com/spatie/laravel-tinker-tools/blob/098e595/src/ShortClassNames.php#L8)\n\nOur class will also [register an autoloader](https://github.com/spatie/laravel-tinker-tools/blob/098e595/src/ShortClassNames.php#L33). When you use `NewsItem` in your code. PHP will first call Composer's autoloader. But of course that autoloader can't find the class. So the autoloader from this package comes next. Our autoloader will use the aforementioned `$classes` collection to find to fully qualified class name. It will then [use `class_alias`](https://github.com/spatie/laravel-tinker-tools/blob/098e595/src/ShortClassNames.php#L46) to alias `NewsItem` to `App\\Models\\NewsItem`.\n\n## What happens if there are multiple classes with same name?\n\nNow you might wonder what'll happen it there are more classes with the same name in different namespaces? E.g. `App\\Models\\NewsItem`, `Vendor\\PackageName\\NewsItem`. Well, `autoload_classmap.php` is sorted alphabetically on the fully qualified namespace. So `App\\Models\\NewsItem` will be used and not `Vendor\\PackageName\\NewsItem`.\n\nBecause `App` starts with an \"A\" there's a high chance that, in case of a collision, a class inside your application will get picked. Currently there are no ways to alter this. I'd accept PRs that make this behaviour customizable.\n\n## Need more Tinker magic?\n\nThere are a lot of other options that can be set in `tinker.config.php`. Learn all the options by reading [the official psysh configuration documentation](http://psysh.org/#configure).\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Postcardware\n\nYou're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\n\nOur address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.\n\nWe publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\nWe got the idea for `ShortClassnames` by reading the \"Tailoring Tinker with custom config\" section of [Caleb Porzio](https://twitter.com/calebporzio)'s excellent blogpost \"[Supercharge Your Laravel Tinker Workflow](https://blog.tighten.co/supercharge-your-laravel-tinker-workflow)\".\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-tinker-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-tinker-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-tinker-tools/lists"}