Home Wiki > openSUSE:Packaging PHP
Sign up | Login

openSUSE:Packaging PHP

tagline: From openSUSE


This page is a introduction on how to package PHP7 projects for openSUSE and others using the openSUSE Build Service. All global guidelines for packaging are also valid for PHP projects.
Warning This page is currently under development. Please test and suggest improvements.

General Notes

To simplify reviewing, please follow all openSUSE packaging guidelines.

  • Use spec-cleaner to verify your spec-file and to bring them into easily readable formats.
  • Check the package's license and ensure it's LICENSE file is included: [1]
  • Check the group. Web applications will typically be installed into Productivity/Networking/Web/Utilities, libraries into Development/Libraries/Other. openSUSE:Package_group_guidelines#Networking

There are some useful macros for packaging PHP available:

Macro Typical value explanation
 %__php /usr/bin/php PHP binary
 %__phpize /usr/bin/phpize PHP-ize binary
 %__php_config /usr/bin/php-config PHP-config binary
 %php_version  ? PHP's version

Packaging Applications

PHP applications which bases on PEAR packages must require those. In case applications should be useable in PHP5 and PHP7, compatible dependencies should be used. Applications that are offered for PHP7 should only consume the virtual provides php-pear(channelname/packagename) instead of the real package names.

PEAR Packaging

To package pear-projects several macros can be used. Starting in September 2017 Thumbleweed will have those macros integrated in the php7-devel package. Older packages must manually include the php7-macros build-dependency.

To let the package installer automatically select a matching PHP7 or PHP5 package it is necessary to explicitly require php5 or php7 in every pear package instead of the generic php package.

Macro Typical value explanation
 %__pear /usr/bin/pear PEAR binary
 %php_pearxmldir /var/lib/pear PEAR's XML directory
 %pear_phpdir /usr/share/php7/PEAR PEAR's main directory
 %pear_docdir /usr/share/php7/PEAR/doc PEAR documentation directory
 %pear_testdir /usr/share/php7/PEAR/test PEAR documentation directory
 %pear_datadir /usr/share/php7/PEAR/data PEAR data directory
 %pear_cfgdir /usr/share/php7/PEAR/cfg PEAR configuration directory
 %pear_wwwdir /usr/share/php7/PEAR/htdocs PEAR htdocs directory
 %pear_metadir /usr/share/php7/PEAR PEAR meta directory

Packaging a PEAR channel

PEAR Packages which do not come from the official PEAR repository must provide a channel to allow installation. Package names for channels should be lower-case and must follow this naming schema: php7-pear-channel-{channel-name}.

Every pear-channel must contain:

BuildRequires:  php7-devel
BuildRequires:  php7-pear
%if 0%{?suse_version} < 1330
BuildRequires:  php7-macros
%endif
Requires:       php7-pear
Provides:       php-channel(channel-name)
BuildRequires:  php7-devel
BuildArch:      noarch

From PHP5 to PHP7

To allow a smooth upgrading from PHP5 versions the package should contain something like the following line:

Obsoletes:      php5-pear-channel-oldchannelname

SPEC file Example

Name:           php7-pear-channel-htmlpurifier
Version:        1.1
Release:        0
Summary:        Adds HTML Purifier channel to PEAR
License:        LGPL-3.0
Group:          Development/Libraries/Other
Url:            http://htmlpurifier.org/
Source0:        http://htmlpurifier.org/channel.xml
BuildRequires:  php7-devel
BuildRequires:  php7-pear
Requires:       php7-pear
Provides:       php-channel(htmlpurifier)
Obsoletes:      php5-pear-channel-htmlpurifier
BuildArch:      noarch
%if 0%{?suse_version} < 1330
BuildRequires:  php7-macros
%endif

%description
This package adds the HTML Purifier htmlpurifier.org channel which
allows PEAR packages from this channel to be installed.

%prep
%setup -q -c -T

%build
# Empty build section, nothing to build

%install
mkdir -p %{buildroot}%{php_pearxmldir}
install -pm 644 %{SOURCE0} %{buildroot}%{php_pearxmldir}/htmlpurifier.org.xml

%post
if [ $1 -eq  1 ] ; then
   %{__pear} channel-add %{php_pearxmldir}/htmlpurifier.org.xml > /dev/null || :
else
   %{__pear} channel-update %{php_pearxmldir}/htmlpurifier.org.xml > /dev/null ||:
fi

%postun
if [ $1 -eq 0 ] ; then
   %{__pear} channel-delete htmlpurifier.org > /dev/null || :
fi

%files
%{php_pearxmldir}/*

%changelog

Packaging a PEAR package

Package names for PEAR packages should be lower-case and must follow this naming schema: php7-pear-{package-name}.

Every PEAR package must contain the following code:

BuildRequires:  php-channel(channel-name)
BuildRequires:  php7-devel
BuildRequires:  php7-pear
%if 0%{?suse_version} < 1330
BuildRequires:  php7-macros
%endif
BuildRequires:  php7-pear
Requires:       php7-pear
Requires:       php-channel(channel-name)
Provides:       php-pear(channel-name/package-name) = %{version}
BuildArch:      noarch

For packages from the main PEAR repository the channel-name is empty

The %{php_pear_gen_filelist} macro can be used to generate file-lists to be included in %files -f %{name}.files.

From PHP5 to PHP7

Support automatic upgrading from PHP5 needs some work. The package must provide all names foreign packages has been unsed. To allow a smooth upgrading from PHP5 versions the package should contain line like the following:

Obsoletes:      php5-pear-old-package-name

Applications that are offered for PHP7 only should consume the virtual provides php-pear(channel-name/package-name) instead of the real package names.

SPEC file Example

%define pear_name      HTMLPurifier
%define pear_sname     htmlpurifier
Name:           php7-pear-htmlpurifier
Version:        4.9.2
Release:        0
Summary:        Standards-compliant HTML filter library written in PHP
License:        LGPL-2.1+
Group:          Development/Libraries/Other
Url:            http://htmlpurifier.org/
Source0:        http://htmlpurifier.org/get/%{pear_name}-%{version}.tgz
#Source1:        LICENSE
BuildRequires:  fdupes
BuildRequires:  php-channel(htmlpurifier)
BuildRequires:  php7-devel
Requires:       php-channel(htmlpurifier)
Requires:       php-xmlreader
Requires:       php-xmlwriter
Requires:       php7-pear
Provides:       php-pear-%{pear_name} = %{version}
Provides:       php-pear(htmlpurifier/%{pear_name}) = %{version}
Obsoletes:      php5-pear-%{pear_name}
BuildArch:      noarch
%if 0%{?suse_version} < 1330
BuildRequires:  php7-macros
%endif

%description
HTML Purifier will not only remove all malicious code (better known as XSS)
with a thoroughly audited, ...

%prep
%setup -q -c
mv package.xml %{pear_name}-%{version}
# cp %%{SOURCE1} .

%build

%install
pushd %{pear_name}-%{version}
%{__pear} install --nodeps --offline --packagingroot %{buildroot} package.xml
install -D -m 0644 package.xml %{buildroot}%{php_pearxmldir}/%{pear_name}.xml
rm -rf %{buildroot}/%{pear_phpdir}/{doc,tmp}
rm -rf %{buildroot}/%{pear_phpdir}/.{filemap,lock,registry,channels,depdb,depdblock}
popd

%{php_pear_gen_filelist}

%post
if [ "$1" = "1" ]; then
  # install (rpm -i)
  %{__pear} install --nodeps --soft --force --register-only %{php_pearxmldir}/%{pear_name}.xml ||:
fi
if [ "$1" = "2" ]; then
  # update (rpm -U)
  %{__pear} upgrade --offline --register-only %{php_pearxmldir}/%{pear_name}.xml ||:
fi

%postun
if [ "$1" -eq "0" ] ; then
# uninstall (rpm -e)
%{__pear} uninstall --nodeps --ignore-errors --register-only %{pear_name} >/dev/null ||:
fi

%files -f %{name}.files
#%%doc LICENSE

%changelog