Download the PHP package rodnaph/singer without Composer
On this page you can find all versions of the php package rodnaph/singer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package singer
Singer, PHP Threading
PHP provides lots of utilities, but calling them in an understandable way usually leads to a mess of code. Singer tries to clean this up by allowing 'threading' of results through functions/methods, giving you a readable chain of business logic.
Threading
By 'threading' I don't mean operating system threads, I mean the threading of the result of one function/method into the next. With PHP we usually need to either assign the results of function calls to temporary variables (to pass them in to the next function)…
Or we nest our function calls so the results go straight into the next…
This is a very simple example, the more steps your have in your data processing the worse it gets.
Singer Usage
Taking the above simple example, this is how you can rewrite it using Singer.
So, Singer allows stitching together of functions, so our code doesn't need to be concerned with handling temporary variables to control the flow of the data. Instead of following variable assignments and tracking them being fed back into other functions, the flow of processing is much more evident.
Thread First/Last
There are a few different methods of threading that Singer allows. The default is called 'thread last' - which means that the result of each function is passed as the last argument to the next function.
The second way you can thread data is called 'thread first' - and means that the result of each function is passed as the first parameter to the next in the chain.
You can switch between these using threadFirst and threadLast methods, as shown below.
In this example we're calling our own array_last function, and also the core PHP function range.
Thread last is meant to operate on functions which take an array ("sequence" functions like array_map), while thread first is meant to work with functions that operate on a single value (eg. str_to_upper).
Thread to nth
You can also thread into arbitrary positions with threadNth
This uses 1-based indexing. So 1 for the first argument position, 2 for the second, etc...
Namespaces
When calling functions on the threader by default it'll look for those functions in the root namespace. Which is fine for the standard library functions. To change the namespace, use the inNamespace method.
Again, you can change the namespace scope arbitrarily.
Static/Object Methods
Until now we've just been calling plain old functions, but you can also call class and instance methods too.
And as usual, these can be included as needed during execution.
Two constructors
There are two constructors for creating a Singer object, the first is create
Which will create a threading object, using thread last, in the root namespace. You can use this right away to call all the PHP standard library functions.
The second constructor is singer
Which creates a threading object using thread last, in the utilities namespace. This namespace provides a bunch of function which are meant to provide a cleaner and more consistent API than PHP's.
Both these methods create the same threader object, just with different default namespaces. You can switch them about with all the options above.
Debugging...
It can be useful to check the value of the context that is currently being processed at different points. To do this you can use the debug function.
If you don't want to use the default print_r then you can pass your own function which will be given the context.
Isn't this like...
Chaining? No, chaining is a technique of calling multiple methods on the same object.
Undercore.js chaining? More like this yeah, but with the control to thread first/last and nth.
What doesn't Singer do?
Singer works best when you're using a more functional approach to writing PHP, and if you use the functions in the utilities namespace (I think) it works quite nicely.
While it does support calling method on objects, if you do find yourself doing this a lot then maybe you should think if it's really the most understandable thing (as your logic is probably to spread across disparate objects... or something...).
Installation
To install Singer you can either just clone/download from Github, or install through Composer.
About
The idea behind Singer is to try to encourage separation of functionality into the most isolated parts possible. Then allowing you to easily pull these bits together at the last possible moment.
If you'd like to contribute ideas or code just open an issue or pull request.