# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit git-r3 shell-completion

DESCRIPTION="Dependency manager for PHP"
HOMEPAGE="https://getcomposer.org https://github.com/composer/composer"

# To regenerate the vendor tarball:
#   git clone -b ${PV} https://github.com/composer/composer ${P}
#   ( cd ${P} && composer install --no-dev --optimize-autoloader )
#   tar --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner \
#       -cf - ${P}/vendor | xz -9e >"${P}-vendor.tar.xz"
SRC_URI="https://github.com/vklimovs/portage-overlay/releases/download/${P}-vendor.tar.xz/${P}-vendor.tar.xz"

# git-r3 rather than the release tarball: bin/compile stamps the phar version
# through git rev-list/describe, and GitHub's git-archive honors export-ignore,
# which strips CHANGELOG.md, README.md and doc/ out from under DOCS.
EGIT_REPO_URI="https://github.com/${PN}/${PN}"
EGIT_COMMIT="f0de0bf90226853b841672f086d8b58b02332504"

LICENSE="MIT"
SLOT="0"
KEYWORDS=""
IUSE="doc"

RDEPEND=">=dev-lang/php-8.2:*[cli,phar]"
BDEPEND="${RDEPEND}"

DOCS=( CHANGELOG.md README.md )

src_unpack() {
	git-r3_src_unpack
	unpack "${P}-vendor.tar.xz"
}

src_compile() {
	php -d memory_limit=-1 -d phar.readonly=0 bin/compile || die
	mv composer.phar composer || die
	php composer completion bash > completion.bash || die
}

src_test() {
	if has usersandbox ${FEATURES} || has network-sandbox ${FEATURES}; then
		ewarn "Skipping smoke test: requires writable user namespaces and"
		ewarn "packagist.org network access, both denied by"
		ewarn "FEATURES=usersandbox / FEATURES=network-sandbox."
		return 0
	fi

	local td="${T}/integration-test"
	mkdir "${td}" || die
	cd "${td}" || die
	php "${S}/composer" init \
		--no-interaction \
		--type=project \
		--name='gentoo/test' \
		--description='Composer ebuild integration test' \
		--license='GPL-3.0-or-later' \
		--require='symfony/console:*' || die
	php "${S}/composer" update --no-interaction --no-progress --prefer-dist || die
	php "${S}/composer" validate --no-interaction || die
}

src_install() {
	dobin composer
	newbashcomp completion.bash composer
	einstalldocs
	use doc && dodoc -r doc
}