Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting to delete when an object is destroyed #70

Merged
merged 2 commits into from
Sep 25, 2023

Conversation

cosmastech
Copy link
Contributor

I know that there was a similar PR for for this a few years ago, but I figured I'd take a run at it.

This is not a breaking change. This adds a simple property so that the directory is deleted when __destruct() is called (which of course happens when the object is out of scope, the application ends, or the user calls unset() on a TemporaryDirectory instance).

function copyFiles(string $directoryName) {
    $temporaryDirectory = (new TemporaryDirectory('/tmp/'))
        ->name($directoryName)
        ->deleteWhenDestroyed()
        ->create();

    // ... do cool stuff with that temporary directory
}

copyFiles('this-will-be-gone');

file_exists('/tmp/this-will-be-gone'); // false

@openjck
Copy link
Contributor

openjck commented Sep 22, 2023

Wow, what good timing. Our team has a need for this, and I was just about to open an issue about it. I'll do a quick review. I can't merge this because I'm not associated with the author.

Copy link
Contributor

@openjck openjck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. The only thing missing might be an update to the documentation which provides an example.

->deleteWhenDestroyed()
->create();

$this->assertDirectoryExists($fullPath = $temporaryDirectory->path());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting. I didn't know PHP could assign while passing an argument. I have to admit it confused me at first, though, and I'll leave it to @spatie to decide if it's a desirable practice.

@freekmurze
Copy link
Member

@cosmastech could you also PR a simple example on how to use the new method to the readme? Thanks!

@freekmurze freekmurze merged commit efc258c into spatie:main Sep 25, 2023
@cosmastech
Copy link
Contributor Author

could you also PR a simple example on how to use the new method to the readme? Thanks!

#74 Done. @freekmurze

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants