BrianHenryIE / strauss

Prefix PHP namespaces and classnames to allow multiple versions of libraries to exist without conflict.
https://brianhenryie.github.io/strauss/
MIT License
144 stars 23 forks source link

Add an option to copy non-php files (/folders) #31

Open Spreeuw opened 3 years ago

Spreeuw commented 3 years ago

When utilizing libraries that contain files like fonts (tecnickcom/tcpdf) or other non-php assets (composer/ca-bundle is a library built around a cacert.pem file), these assets are not copied over by Strauss. We currently resolve this with a bash script that runs after Strauss, but it would be nice to be able to tell Strauss which folders to copy over, or even to let it copy all the files/folders for a specific library?

BrianHenryIE commented 3 years ago

You can use override_autoload to add a files key to the package's existing autoload configuration with the extra files you want.

Adding the res folder to composer/ca-bundle is straightforward:

{
  "require": {
    "composer/ca-bundle": "1.2.10"
  },
  "require-dev": {
    "brianhenryie/strauss": "0.10.4"
  },
  "extra": {
    "strauss": {
      "target_directory": "strauss",
      "namespace_prefix": "Issue31\\",
      "override_autoload": {
        "composer/ca-bundle": {
            "psr-4": {
              "Composer\\CaBundle\\": "src"
            },
            "files": [
              "res"
            ]
        }
      }
    }
  }
}

tecnickcom/tcpdf's fonts folder contains PHP files that you probably don't want to prefix, so add that path to the exclude_from_prefix/file_patterns key.

{
  "require": {
    "tecnickcom/tcpdf": "6.4.2"
  },
  "require-dev": {
    "brianhenryie/strauss": "0.10.4"
  },
  "extra": {
    "strauss": {
      "target_directory": "strauss",
      "namespace_prefix": "Issue31\\",
      "override_autoload": {
        "tecnickcom/tcpdf": {
          "classmap": [
            "config",
            "include",
            "tcpdf.php",
            "tcpdf_parser.php",
            "tcpdf_import.php",
            "tcpdf_barcodes_1d.php",
            "tcpdf_barcodes_2d.php",
            "include/tcpdf_colors.php",
            "include/tcpdf_filters.php",
            "include/tcpdf_font_data.php",
            "include/tcpdf_fonts.php",
            "include/tcpdf_images.php",
            "include/tcpdf_static.php",
            "include/barcodes/datamatrix.php",
            "include/barcodes/pdf417.php",
            "include/barcodes/qrcode.php"
          ],
          "files": [
            "fonts"
          ]
        }
      },
      "exclude_from_prefix": {
        "file_patterns": [
          "/^psr.*$/",
          "~^tecnickcom/tcpdf/fonts.*~"
        ]
      }
    }
  }
}

TBH, most of the classmap they have defined is unnecessary here. Once the directory is specified, everything inside it will be copied.

I'll leave this open until I update the README with a few lines about this.

Spreeuw commented 3 years ago

Thanks for working that out! I think we'll stick to copying afterwards in that case, for simplicity sake. Configuring the autoloader part feels like an unnecessary extra step if all you want is the files to be copied.

rickmacgillis commented 2 years ago

Excellent. This worked well for copying directories I need. Thank you @BrianHenryIE.

Stripped down version:

"extra": {
    "strauss": {
      "override_autoload": {
        "composer/ca-bundle": {
            "files": [
              "res"
            ]
        }
      }
    }
  }
BrianHenryIE commented 1 year ago

I'm considering this functionality, maybe like:

"extra": {
    "strauss": {
      "copy_files": {
        "composer/ca-bundle": [
            "res"
          ]
      }
    }
  }

It would really just be merged back into the override_autoload key mentioned above, but it's a little clearer.

What's the best name? extra_files, include_files, additional_files, just files?

Spreeuw commented 1 year ago

@alexmigf

alexmigf commented 1 year ago

@BrianHenryIE extra_files or additional_files seems fine to me.

UVLabs commented 1 year ago

@BrianHenryIE I had need for something like this due to how Dompdf works. By default strauss does not copy some of the root files. The VERSION file is actually needed by dompdf or else it throws an error when using the library.

See this line: https://github.com/dompdf/dompdf/blob/v2.0.3/src/Dompdf.php#L261

I had to the following to get all the files into the prefixed directory:

"override_autoload": {
 "dompdf/dompdf": {
  "classmap": ["."]
 }
}