Ralph J. Smit Laravel & PHP-developer.
Creating packages for PHP or Laravel is a great way to speed up manual processes and reuse code, applying the principles of Don't Repeat Yourself (DRY) across all your projects. Sometimes you'd want a package to move several files to a new location or update namespaces. This package aims to help you with simplifying complex stub workflows. Let's dive in👇
Last week I launched a new big Laravel package, called ralphjsmit/tall-install. This package can automate the full installation process of a Laravel-application according to the TALL-stack.
It can also configure your project so that it uses a Domain-Driven file structure (DDD). This involves moving multiple files around and changing namespaces.
This would become very boring and unmaintainable quickly, so I decided to put that functionality in a separate package, called
ralphjsmit/filesystem. Check it out on GitHub🚀.
What this package basically does, is giving you an easy and fluent way to move files and perform other file actions. Let's see it in action to clarify this.
First, install the package:
composer require ralphjsmit/filesystem
Creating a stub configuration
To start moving files, we'll first need to create a so-called
Stub configuration. Later, you can use a stub configuration to select a file and do actions on the file.
Stub configuration can contain two things:
A base directory, so that you can use relative paths instead of absolute paths
An array of namespaces and the directories of them.
Here are a few examples:
use RalphJSmit\Filesystem\Stub;$stub = Stub::dir(__DIR__);// Creates a Stub configuration that has the __DIR__ as the root directory.// All other file names and directories can be relative to the root directory, like so:$stub->getFile('/tmp/testFileA.php')->move('/tmp/otherFolder');// You can also map namespaces to a stub configuration:$stubs = Stub::dir(__DIR__)->namespaces(['Support' => '/src/Support/','Domain' => '/src/Domain/','App' => '/src/App/',]);// When moving a file into a new namespace, you can now define the namespace the file// should be added to, and the Stub configuration will automatically look up the// correct folder.$stubs->getFile('/tmp/TestFileA.php')->namespace('Support/Models');// Moves __DIR__ . `/tmp/testFileA.php` to __DIR__ . `/src/Support/Models/testFileA.php`.
Getting a file object
Now that you have a stub configuration, you can now retrieve a file object. A file object is a class on which you can call fluent methods to move and copy files.
You can get a plain file object, or get a file object from a
$file = Stub::file(__DIR__ . '/tmp/testFileA.php');$stub = Stub::dir(__DIR__);$file = $stub->getFile('/tmp/testFileA.php');
Performing fluent actions on a file object
Now that you have a file object, you can perform fluent actions on it. You can copy or move a file to a new directory with the
$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->copy('/tmp/test');// $file is now in __DIR__ . '/tmp/testFileA.php' AND in __DIR__ . '/tmp/test/testFileA.php'$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->move('/tmp/test');// $file is now in __DIR__ . '/tmp/test/testFileA.php'
In total, you can perform the following actions on a file:
As you can see, there are many filesystem actions you can take with a file object. In fact, I see this as a nice and fluent
Symfony/Filesystem alternative, that allows you to reuse initial configurations and perform actions on the files.
I had a lot of fun writing this package and it has certainly been useful for me in creating my Tall Install package. I hope you will benefit from the package as well!
If it was useful for you, I’d highly appreciate if if you could spare a minute and star the repo on GitHub✨.
Feel free to open an issue or leave a comment with your ideas and questions!
Hey!👋 Like what you read?
Every now and then I send out a newsletter with new articles, tutorials and packages, mainly related to PHP in general and Laravel.
Subscribe to the newsletter and stay up to date with all things Laravel, Tailwind, WordPress & PHP. Never miss an update.🙋