php - 如何创建包含时自动生成代码的 Composer 库包

标签 php github package composer-php auto-generate

每次我创建一个新的 PHP 项目时,我基本上都会使用我所采用和喜欢的相同的 MVC 文件夹结构,我使用相同的基类、接口(interface)和相同的 PDO DAL 实现。 当我创建一个新项目时,除了一些更改之外,我还将所有需要的文件复制并粘贴到新项目中,例如更改命名空间(以匹配新项目名称)等。

我想,为什么不创建一个简单的脚本来复制这些文件和文件夹,并进行其他更改。 所以现在,当我创建一个新项目时,我只需运行脚本,代码就会自动生成,这要好得多。

然后我想,我希望它变得更简单。我不想将代码保存在我的电脑中,我想将其保存在Github上,并且由于我使用并且喜欢composer,所以我想我会将Github项目制作为composer包。 但是当尝试实现它时,我意识到我无法让新的 Composer 包自动生成我想要的代码,或者至少我不知道如何让它做到这一点。 我尝试用谷歌搜索它但没有成功。

有谁知道如何实现这个目标吗?

最佳答案

我认为没有必要在这里生成代码。只需将骨架文件添加到 Git 存储库并使用 Composer 的 create-project特征。参见第三点:

You can use Composer to create new projects from an existing package. This is the equivalent of doing a git clone/svn checkout followed by a composer install of the vendors.

There are several applications for this:

  1. You can deploy application packages.
  2. You can check out any package and start developing on patches for example.
  3. Projects with multiple developers can use this feature to bootstrap the initial application for development.

支持此方法的主要 PHP 项目的示例是 Laravel 。来自其installation instructions :

Via Composer

The Laravel framework utilizes Composer for installation and dependency management. If you haven't already, start by installing Composer.

Now you can install Laravel by issuing the following command from your terminal:

composer create-project laravel/laravel your-project-name --prefer-dist

This command will download and install a fresh copy of Laravel in a new your-project-name folder within your current directory.

If you prefer, you can alternatively download a copy of the Laravel repository from Github manually. Next run the composer install command in the root of your manually created project directory. This command will download and install the framework's dependencies.

编辑:

要在 composer create-project 完成后运行脚本,您应该能够使用 Composer script :

A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Scripts are useful for executing a package's custom code or package-specific commands during the Composer execution process.

您可能正在寻找 post-create-project-cmd 事件:

occurs after the create-project command is executed

根据您的脚本是静态 PHP 方法还是 shell 可执行文件,您应该按如下方式定义它(根据 Composer 文档修改):

{
    "scripts": {
        "post-create-project-cmd": "MyVendor\\MyClass::postUpdate"
    }
}

{
    "scripts": {
        "post-create-project-cmd": "my-shell-script arg1 arg2"
    }
}

关于php - 如何创建包含时自动生成代码的 Composer 库包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24201639/

相关文章:

php - zend 表单验证

php - 如何完成此 Google Calendar Api v3 - FreeBusy PHP - 示例?

git - 在 GitFlow 中剪切发布分支的实际命令

entity - VHDL 你能在同一个文件中声明一个包和一个实体吗?

java - 用户定义的包和类路径

php - 如何以应用程序仍可读的方式安全地存储密码?

javascript - 在另一个调用正在使用 php (Laravel) 执行长时间作业时创建一个 ajax

用于机器人的 Python : How to generate an app for localization of a Pepper robot

Github:每次使用后我都必须制作一个新的 SSH key

java - Java项目的包结构?