PHP code example of ralphjsmit / filesystem

1. Go to this page and download the library: Download ralphjsmit/filesystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ralphjsmit / filesystem example snippets


use RalphJSmit\Filesystem\Stub;

$stub = Stub::new();

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

$stub = Stub::dir(__DIR__);

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

$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`.

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

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

$stub = Stub::dir(__DIR__);

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

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

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

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

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

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

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

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

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

$newContents = 'Hello world!';

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

$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

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

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

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