独自のcomposerパッケージの作成

4904 ワード

composer/packagistパッケージの作成
 
githubの上に新しいライブラリを作成し、carと名付けてreadmeを初期化します.mdをローカルにクローンします.
git clone https://github.com/maxwelldu/car.git
cd car

 
 
ここのcarフォルダはパッケージのrootディレクトリで、composerを覚えるだけです.jsonはパッケージのどのディレクトリの下にありますか.一般的にはパッケージのrootディレクトリです.
まだだjsonファイルは、コマンド:composer init、中括弧の中のはデフォルト値で、直接車に戻ると中の値が使用されます.
 
➜  car git:(master) composer init


  Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (/) [maxwelldu/car]:
Description []: composer study
Author [maxwelldu ]:
Minimum Stability []:
Package Type []:
License []:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

 
composer.jsonは以下のように変更されました.
 
{
    "name": "maxwelldu/car",
    "description": "composer study",
    "license": "MIT",
    "authors": [
        {
            "name": "maxwelldu",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {
            "Ford\\Escape\\": "src/Ford/Escape",
            "Ford\\Fusion\\": "src/Ford/Fusion",
            "Ford\\Focus\\": "src/Ford/Focus",
            "Ford\\Fiesta\\": "src/Ford/Fiesta"
        }
    }
}

 
 
上のネーミングスペースとディレクトリのマッピング関係に基づいて、パッケージの構造は次のようになります.
 
|____.gitignore
|____composer.json
|____composer.lock
|____README.md
|____src
| |____Ford
| | |____Escape
| | | |____Escape2013.php
| | |____Fiesta
| | | |____Fiesta2013.php
| | |____Focus
| | | |____Focus2013.php
| | |____Fusion
| | | |____Fusion2013.php
 
Escape2013.php
 
";
    }
}

 
Fiesta2013.php
 
";
    }
}

 
 
Focus2013.php
 
";
    }
}

 
Fusion2013.php
 
";
    }
}

 
以上を整理した後、composerをインストールして、私たちのパッケージが正常に動作するかどうかをテストする必要があります.インストールは簡単です.パッケージのrootディレクトリの下でinstallすればいいです.
composer install

 
インストール後vendor/composer/autoload_psr4.phpでは、ネーミングスペースとディレクトリのマッピング関係が生成され、1つの配列にパッケージされます.
 
 array($baseDir . '/src/Ford/Fusion'),
    'Ford\\Focus\\' => array($baseDir . '/src/Ford/Focus'),
    'Ford\\Fiesta\\' => array($baseDir . '/src/Ford/Fiesta'),
    'Ford\\Escape\\' => array($baseDir . '/src/Ford/Escape'),
);

 
テストファイルを新規作成しますphp、次の内容で入力します.

 
次に、現在のディレクトリでサーバを起動します.
php -S localhost:8080

 
 
ブラウザを開いて入力:http://localhost:8080/show.php
ブラウザから次のように出力されます.
 
This is Ford Escape2013!
This is Ford Fiesta2013!
This is Ford Focus2013!
This is Ford Fusion2013!
 
そして私たちはショーをします.phpを削除しgithubにコードをプッシュ
 
開くhttps://packagist.org/
githubアカウントでログイン
 
パケットをコミットしgithubのアドレスを[email protected]:maxwelldu/car.git記入
そして提出すればいい