furey / tinx

⛔️ Laravel Tinx is archived and no longer maintained.
449 stars 37 forks source link

Pascal Naming + Shortcuts Table + Refactors #35

Closed furey closed 7 years ago

furey commented 7 years ago

Summary

• Added "pascal" naming strategy/tests (replaces "shortestUnique" as default) • Added console "Class/Shortcuts" table (replaces names(); filterable e.g. names('user')) • Added "illuminate/container:~5.2" dependency (supporting back to Laravel 5.2) • Significant architectural refactors • Update README (including GIF)

Pascal Naming

I've implemented a fairly bullet proof naming strategy which I've simply called pascal as it's based on the first pascal case characters of a model's short class name (but varies from this when naming conflicts occur as it attempts to keep searching for unique variations).

I've written tests demonstrating how the strategy works in /tests/PascalStrategyTest.php.

/src/Naming/PascalStrategy.php is also fully commented for further examination.

The shortestUnique source has moved to /src/Naming/ShortestUniqueStrategy.php.

Strategy Interface

Both strategies now implement the new Ajthinking\Tinx\Naming\Strategy interface.

Users can create their own Strategy implementation and hook it into Tinx by setting their implementation's full class name as their strategy config value like so:

<?php

// 'config/tinx.php'

return [

    // etc…

    /**
     * Model shortcut naming strategy (e.g. 'App\User' = '$u', 'u()').
     * Supported: 'pascal', 'shortestUnique'
     * Also supports any resolvable full class name implementing 'Ajthinking\Tinx\Naming\Strategy'.
     * */
    'strategy' => 'My\Custom\Strategy\Implementation',

    // etc…

];

Shortcuts Table

I'm now displaying a table of user "Class/Shortcuts" when Tinx loads:

table

These shortcuts only appear on load if a user's session satisfies their names_table_limit config value (defaults to 10). This value exists so projects with a huge number of models don't get smashed by a massive list of shortcuts on load, unless the user specifically sets that names_table_limit value to -1 (or boots Tinx with a verbose flag, e.g. php artisan tinx -v).

names()

This table also displays when the names() function is called during a session (replacing the previous implementation which simply dumped $names):

names

The names() function also accepts string filters as parameters which if passed filters the rows displayed by the shortcuts table:

names-filters

Architectural Refactors

Apologies for all the refactoring! I went down the rabbit implementing "pascal" naming, but the functional benefits should outweigh the refactor costs.

Next Steps

Hi @ajthinking,

When you get time, can you please have a play and let me know how you go.

If approved, I suppose these changes necessitate a full point release?

Cheers!

🤓👍

ajthinking commented 7 years ago

Hi @furey sorry for the delay, unfortunately had IRL events to manage. But I was very happy to see this PR! I have tested it out in some old projects and it seems to work just as intended :) Awesome tests by the way! And there was reason for refactoring so thats all good.

You beat me to making a new gif also. Darnit, next time it should feature you and Jon Snow haha! Thanks man!

ajthinking commented 7 years ago

@furey I just pushed 1.0.0, hope you dont mind we skipping 0.9!

furey commented 7 years ago

Hey @ajthinking,

A 1.0.0 release is a great idea – nice one!

The next feature I'm looking at implementing is recursive model searching, so if you set a namespaces_and_paths record with an asterisk at the end (e.g. ''/app/Models/*'), Tinx will recursive search all directories within to sniff out all your models. Sound cool?

Hey while I think of it, can I propose the following tweaks to the repo overview (feel free to ignore if not appropriate)?

Reload your Laravel Tinker session from inside Tinker, plus automatic shortcuts for first(), find(), search, and more!

E.g.

repo-proposal

Cheers!

ajthinking commented 7 years ago

Recursive model searching sounds great! I dont know if its good or bad so just consider it as inspiration: I have seen this syntax in other software:

all/files/in/folder/*
all/files/in/folder/recursive/**/*

Topics and desc: Good idea, added!

Cheers ^ ^

furey commented 7 years ago

@ajthinking Ending in an asterisk makes perfect sense. Cheers!