phillip-elm/temp-workspaces

用于创建临时工作目录的实用类,在销毁时会删除自身。

v1.1.1 2018-11-03 17:05 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:03 UTC


README

这是一个简单的库,旨在简化临时目录的快速创建。

当对象被销毁时,临时目录及其内容将被删除。

安装

composer require phillip-elm/temp-workspaces

基本用法

<?php

use PhillipElm\TempWorkspaces\TempDirectoryWorkspace;

function example()
{
  // This creates a workspace and returns it.
  $workspace = TempDirectoryWorkspace::create();

  // This will make a new directory within your workspace.
  $workspace->mkdir('example/test', 0777, true);

  // This will return the full path of the given sub-path relative to the workspace root.
  $path = $workspace->path('example/test/test.txt');

  $workspace->putContents('example/test/test.txt', 'This is a test');

  echo $workspace->getContents('example/test/test.txt');

  // When called without an argument, path() returns the root of the workspace without a trailing slash.
  echo $workspace->path();

  // Once $workspace is destructed, the temporary directory and its contents will be purged.
}

文档

这个库是匆忙中拼凑起来的。因此,没有正式生成的PHPDoc或指南。

项目分解为以下接口/类

  • src/Contracts/DirectoryWorkspace.php
    • 包含可以调用的对象方法头部的接口
  • src/BaseDirectoryWorkspace.php
    • 实现DirectoryWorkspace接口中包含的功能
  • src/TempDirectoryWorkspace.php
    • 扩展了BaseDirectoryWorkspace,包含销毁时清理的功能。
  • src/DirectoryRemover.php
    • 一个简单的类,用于递归删除目录而不使用exec

此外,您可以查看测试用例,以获取更多关于如何使用此库的示例。