Coder Social home page Coder Social logo

filesystem's Introduction

Stubs Banner

A fluent filesystem package for PHP

This package helps you to speed up the process of moving and copying files. It also makes replacing namespaces much easier, thus making it an invaluable tool for heavy filesystem tasks. Enjoy!

Run Tests

Installation

You can install the package via composer:

composer require ralphjsmit/filesystem

Usage

This package works by providing you with a base Stub class and a File class. As the name implies, the Stub class is an object that contains specific configuration, like the namespaces and the base directory. The File class is used to represent an individual file.

Creating a Stub configuration

You can create a new Stub configuration with Stub::new():

use RalphJSmit\Filesystem\Stub;

$stub = Stub::new();

You can use a Stub configuration like this (I'll talk more about file actions below):

$stub->getFile(__DIR__ . '/tmp/testFileA.php')->move(__DIR__ . '/tmp/otherFolder');

You can also set a base directory for your Stub:

$stub = Stub::dir(__DIR__);

$stub->getFile('/tmp/testFileA.php')->move('/tmp/otherFolder');

If you already have a $stub instance, you can configure namespaces on them. Those namespaces are used on the File object for the ->namespace() action. It basically means that you define the directories for each namespace in your project.

$stubs = Stub::dir(__DIR__)->namespaces([
    'Support' => '/src/Support/',
    'Domain' => '/src/Domain/',
    'App' => '/src/App/',
]);

$stubs->getFile('/tmp/TestFileA.php')->namespace('Support/Models');
// Moves __DIR__ . `/tmp/testFileA.php` to __DIR__ . `/src/Support/Models/testFileA.php`.

You can also have multiple stub configurations at the same time:

$stubA = Stub::dir(__DIR__ . '/src');
$stubB = Stub::dir(__DIR__ . '/app');
$stubC = Stub::dir(__DIR__ . '/tmp');

Getting a File object

You can get a File object by directly calling file() on the Stub class:

$file = Stub::file(__DIR__ . '/tmp/testFileA.php');

You can also get a File object from a $stub instance:

$stub = Stub::dir(__DIR__);

$file = $stub->getFile('/tmp/testFileA.php');

Actions on a File object

If you have a File object, you can perform the following actions on it:

Copying a file

You can copy a file to a new directory using the copy() function:

$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'

Deleting a file

You can delete a file using the delete() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->delete();
// returns true on success

Getting the basename of a file

You can get the basename of a file using the getBasename() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getBasename();
// 'testFileA.php'

Getting the directory location of a file

You can get the location of the directory of a file using the getDirectory() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getDirectory();
// __DIR__ . '/tmp'

Getting the full path of a file

You can get the full path of a file using the getFilepath() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getFilepath();
// __DIR__ . '/testFileA.php'

Getting the file contents

You can get the contents of a file using the getContents() function:

$contents = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getContents();

Moving a file

You can move a file to a new directory using the move() function:

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->move('/tmp/test');
// $file is now in __DIR__ . '/tmp/test/testFileA.php'

Updating the namespace of a file

You may update the namespace of a file and move it to the correct directory with the namespace() helper. This is ideal if you need to move a PHP-file to a new directory and update the namespace of it. I use this technique in the ralphjsmit/tall-install package.

$basePath = __DIR__;

$stubs = Stub::dir($basePath)->namespaces([
    'Support' => '/src/Support/',
    'Domain' => '/src/Domain/',
    'App' => '/src/App/',
]);

$stubs->getFile('/app/Console/Kernel.php')->namespace('Support\App\Console');
// file is not in __DIR__ . '/src/Support/App/Console/Kernel.php'

Checkout a real-life example from one of my packages.

Updating the contents of a file

You may update the contents of a file with the putFile() method:

$newContents = 'Hello world!';

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->putFile($newContents);

You may also specify a new location for the file:

$newContents = 'Hello world!';

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->putFile($newContents, '/tmp/test/myFile.php');
// Will create a file with the "Hello world!" in __DIR__ . '/tmp/test/myFile.php`
// Old file will still exist

If you just want to move or copy a file, you should use those methods.

Updating the namespace of a file

You may replace the namespace of a file with the replaceNamespace($newNamespace) method:

$file = Stub::dir(__DIR)->getFile('/tmp/test/MyClass.php');

$file->replaceNamespace('App\Models');

// $file will now have the namespace App\Models

General

๐Ÿž If you spot a bug, please submit a detailed issue and I'll try to fix it as soon as possible.

๐Ÿ” If you discover a vulnerability, please review our security policy.

๐Ÿ™Œ If you want to contribute, please submit a pull request. All PRs will be fully credited. If you're unsure whether I'd accept your idea, feel free to contact me!

๐Ÿ™‹โ€โ™‚๏ธ Ralph J. Smit

filesystem's People

Contributors

ralphjsmit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.