wpkg / wpkg-php
PHP上的WPKG配置生成器
Requires
- php: >=7.0
- ext-xml: *
- adldap2/adldap2: ^8.0
- evilfreelancer/yaml-php: ^0.4.0
- spatie/array-to-xml: ^2.7
- wpkg/wpkg-js: ^1.3.1
Requires (Dev)
- phpunit/phpunit: ^6.0
README
WPKG XML配置生成器
使用PHP7编写的库,用于生成WPKG安装程序的配置XML文件。
composer require drteam/wpkg-php
查看链接获取更多关于WPKG的信息。
如果您需要为生成来自域PC的hosts.xml
的Active Directory支持,可以查看基于此库的WPKG-AD项目。
目录
如何创建XML
一些带有描述的示例可以在这里找到。
配置
为wpkg.js运行时行为配置的设置
Config.xml文件
使用Config类,您可以覆盖设置,如果指定了与默认值不同的值,则您的参数将被添加到XML文件中。
如果您没有指定任何内容,将生成具有默认参数的配置。
$_config = new \WPKG\Config(); // Overwrite some attributes $_config ->with('wpkg_base', 'http://example.com') ->with('quitonerror', true) ->with('debug', true); // Now we can set the variables $_config ->withVariable('PROG_FILES32', "%ProgramFiles%", null, "x86") ->withVariable('PROG_FILES32', "%ProgramFiles(x86)%", null, "x64") ->withVariable('DESKTOP', "%ALLUSERSPROFILE%\Desktop", "Windows xp") ->withVariable('DESKTOP', "%PUBLIC%\Desktop", "Windows 7"); // Show current variant of generated XML echo $_config->show();
执行结果
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:profiles="http://www.wpkg.org/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/config xsd/config.xsd"> <languages> ... a lot of lines with translations ... </languages> <param name="wpkg_base" value="http://example.com"/> <param name="quitonerror" value="true"/> <param name="debug" value="true"/> <variables> <variable name="PROG_FILES32" value="%ProgramFiles%" architecture="x86"/> <variable name="PROG_FILES32" value="%ProgramFiles(x86)%" architecture="x64"/> <variable name="DESKTOP" value="%ALLUSERSPROFILE%\Desktop" os="Windows xp"/> <variable name="DESKTOP" value="%PUBLIC%\Desktop" os="Windows 7"/> </variables> </config>
关于翻译的说明
目前,以下语言提供了翻译(WPKG项目的创建者称它们为语言)
- 英语
- 法语
- 德语
- 意大利语
- 俄语(由我添加)
- 西班牙语
翻译是从wpkg-1.3.1-bin.zip存档中的WPKG项目官方网站的config.xml文件中获取的。
您可以在这里找到所有可用的wpkg-php翻译。
如果您在列表中看不到您的语言,并想帮助该项目,则可以通过问题或PR建议您的翻译变体。请注意LCID,这些是唯一的语言标识符,您可以在这里找到它们的完整列表。
主机
机器名和配置文件名之间的映射。
单个主机
如果您想生成几个主机,并将它们分别存储在文件中
// Root container $_host = new \WPKG\Host(); // Need to add some parameters $_host ->with('name', 'host1') ->with('profile-id', 'profile1'); echo $_host->show();
结果是
<?xml version="1.0" encoding="UTF-8"?> <hosts:wpkg xmlns:hosts="http://www.wpkg.org/hosts" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/hosts xsd/hosts.xsd"> <host name="host1" profile-id="profile1"/> </hosts:wpkg>
您也可以设置配置文件数组
// Root container $_host = new \WPKG\Host(); // Need to add some parameters $_host ->with('name', 'host1') ->with('profile-id', ['profile1', 'profile2', 'profile3']); echo $_host->show();
结果必须是
<hosts:wpkg xmlns:hosts="http://www.wpkg.org/hosts" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/hosts xsd/hosts.xsd"> <host name="host1" profile-id="profile1"> <profile profile-id="profile2"/> <profile profile-id="profile3"/> </host> </hosts:wpkg>
Hosts.xml文件
如果您需要一个包含所有主机的单个大文件
// Root container $_hosts = new Hosts(); /** * Test host #1 */ $host1 = new Host(); $host1 ->with('name', 'host1') ->with('profile-id', 'profile1'); $_hosts->setHost($host1); /** * Test host #2 */ $host2 = new Host(); $host2 ->with('name', 'host2') ->with('profile-id', ['profile1', 'profile2', 'profile3']); $_hosts->setHost($host2); /** * Test host #3 */ $host3 = new Host(); $host3 ->with('name', 'host3') ->with('profile-id', 'profile3'); $_hosts->setHost($host3); echo $_hosts->show();
结果文件hosts.xml位于wpkg_path文件夹中
<?xml version="1.0" encoding="UTF-8"?> <hosts:wpkg xmlns:hosts="http://www.wpkg.org/hosts" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/hosts xsd/hosts.xsd"> <host name="host1" profile-id="profile1"/> <host name="host2" profile-id="profile1"> <profile profile-id="profile2"/> <profile profile-id="profile3"/> </host> <host name="host3" profile-id="profile3"/> </hosts:wpkg>
来自Active Directory的计算机
此类基于adLdap库,因此您可以使用该库的任何配置参数。
基本用法
use \WPKG\Drivers\ADImport; // Read AD configuration $_config = include __DIR__ . '/adldap.php'; // Set Import object for work and put configuration inside $_import = new ADImport($_config); // You also can set config via specific method //$_import->setConfig($_config); // Choose work mode (only hosts available) and output the XML $_hosts = $_import->import('hosts'); $out = $_hosts->show(); print_r($out);
您应该看到以下内容
<?xml version="1.0" encoding="UTF-8"?> <hosts:wpkg xmlns:hosts="http://www.wpkg.org/hosts" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/hosts xsd/hosts.xsd"> <host name="user1.example.com" profile-id="default"/> <host name="user2.example.com" profile-id="default"/> <host name="user3.example.com" profile-id="default"/> </hosts:wpkg>
配置文件
指定每个WPKG配置文件将安装/执行的哪些软件包。
单个配置文件
如果您想生成几个配置文件,并将它们分别存储在文件中
use WPKG\Profile; $_profile = new \WPKG\Profile(); $_profile ->with('id', 'profile1') ->with('packages', 'DotNet') ->with('depends', 'profile2'); // Show current variant of generated config echo $_profile->show();
结果文件(例如当前示例中的profile1.xml)可以在wpkg_path/profiles/子文件夹中找到
<?xml version="1.0" encoding="UTF-8"?> <profiles:profiles xmlns:profiles="http://www.wpkg.org/profiles" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/profiles xsd/profiles.xsd"> <profile id="profile1"> <depends profile-id="profile2"/> <package package-id="DotNet"/> </profile> </profiles:profiles>
您作为主机也可以设置包含 packages
或 depends
的数组。
Profiles.xml文件
如果您需要一个包含所有配置文件的大文件
$_profiles = new \WPKG\Profiles(); /** * Test profile #1 */ $pr1 = new Profile(); $pr1->with('id', 'profile1'); $_profiles->setProfile($pr1); /** * Test profile #2 */ $pr2 = new Profile(); $pr2->with('id', 'profile2') ->with('packages', 'DotNet'); $_profiles->setProfile($pr2); /** * Test profile #3 */ $pr3 = new Profile(); $pr3->with('id', 'profile3') ->with('packages', ['Firefox', 'Chromium', 'Opera']) ->with('depends', 'profile1'); $_profiles->setProfile($pr3); /** * Test profile #4 */ $pr4 = new Profile(); $pr4->with('id', 'profile4') ->with('packages', ['SuperBank', 'AnotherBank']) ->with('depends', ['profile1', 'profile2']); $_profiles->setProfile($pr4); /** * Test profile #5 */ $pr5 = new Profile(); $pr5->with('id', 'profile5') ->with('depends', 'profile3'); $_profiles->setProfile($pr5); // Show current variant of generated config echo $_profiles->show();
结果
<?xml version="1.0" encoding="UTF-8"?> <profiles:profiles xmlns:profiles="http://www.wpkg.org/profiles" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/profiles xsd/profiles.xsd"> <profile id="profile1"/> <profile id="profile2"> <package package-id="DotNet"/> </profile> <profile id="profile3"> <depends profile-id="profile1"/> <package package-id="Firefox"/> <package package-id="Chromium"/> <package package-id="Opera"/> </profile> <profile id="profile4"> <depends profile-id="profile1"/> <depends profile-id="profile2"/> <package package-id="SuperBank"/> <package package-id="AnotherBank"/> </profile> <profile id="profile5"> <depends profile-id="profile3"/> </profile> </profiles:profiles>
软件包
定义软件包(WPKG 安装/卸载程序的命令等)
单个软件包
如果您想将几个包生成在单独的文件中
use \WPKG\Package; use \WPKG\PackageCheckExits; $_package = new Package(); $_exits = new PackageCheckExits(); // Overwrite the attributes of tha class $_package ->with('id', 'wpkg1') ->with('name', 'Windows Packager sample 1') ->with('revision', 1) ->with('priority', 0) ->with('reboot', 'false'); // Small check for Windows 7 $_package ->withCheck('registry', 'exists', 'HKLM\Software\wpkg\full\key\not\part\of\it') ->withCheck('file', 'exists', 'C:\wpkg\wpkg.bat') ->withCheck('uninstall', 'exists', 'WPKG 0.6-test1'); // Add few variables to package config $_package ->withVariable('PROG_FILES32', "%ProgramFiles(x86)%", null, "x64") ->withVariable('DESKTOP', "%ALLUSERSPROFILE%\Desktop", "Windows xp"); // We need set exit codes for some installation stages $_exits ->add(0) ->add(3010, true) ->add('any') ->add(2); // Run command $_package ->withCommand('install', 'msiexec /i /qn "%SOFTWARE%\path\to\msi"', 'test', $_exits) ->withCommand('remove', 'msiexec /x /qn "%SOFTWARE%\path\to\msi"') ->withCommand('upgrade', 'msiexec /i /qn "%SOFTWARE%\path\to\msi"') ->withCommand('downgrade', null, 'remove') ->withCommand('downgrade', null, 'install'); echo $_package->show();
结果
<?xml version="1.0" encoding="UTF-8"?> <packages:packages xmlns:packages="http://www.wpkg.org/packages" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/packages xsd/packages.xsd"> <package id="wpkg1" name="Windows Packager sample 1" revision="1" priority="0" reboot="false"> <check type="registry" condition="exists" path="HKLM\Software\wpkg\full\key\not\part\of\it"/> <check type="file" condition="exists" path="C:\wpkg\wpkg.bat"/> <check type="uninstall" condition="exists" path="WPKG 0.6-test1"/> <variable name="PROG_FILES32" value="%ProgramFiles(x86)%" architecture="x64"/> <variable name="DESKTOP" value="%ALLUSERSPROFILE%\Desktop" os="Windows xp"/> <commands> <command type="install" cmd='msiexec /i /qn "%SOFTWARE%\path\to\msi"' include="test"> <exit code="0"/> <exit code="3010" reboot="true"/> <exit code="any"/> <exit code="2"/> </command> <command type="remove" cmd='msiexec /x /qn "%SOFTWARE%\path\to\msi"'/> <command type="upgrade" cmd='msiexec /i /qn "%SOFTWARE%\path\to\msi"'/> <command type="downgrade" include="remove"/> <command type="downgrade" include="install"/> </commands> </package> </packages:packages>
Packages.xml文件
如果您需要一个包含所有软件包的大文件
use \WPKG\Package; use \WPKG\PackageCheckExits; use \WPKG\Packages; // Root container $_packages = new Packages(); /** * Test package #1 */ $pk1 = new Package(); $pk1->with('id', 'time') ->with('name', 'Time Synchronization') ->with('priority', 100) ->with('execute', 'always') ->withCheck('host', 'os', 'windows 7') ->withCommand('install', 'net time \\timeserver /set /yes'); $_packages->setPackage($pk1); /** * Test package #2 */ $pk2 = new Package(); $pk2_exits = new PackageCheckExits(); // We need set exit codes for some installation stages $pk2_exits ->add(0) ->add(3010, true) ->add('any') ->add(2); $pk2->with('id', 'wpkg') ->with('name', 'Windows Packager sample 1') ->with('revision', 1) ->with('priority', 0) ->with('reboot', 'false') ->withCheck('registry', 'exists', 'HKLM\Software\wpkg\full\key\not\part\of\it') ->withCheck('file', 'exists', 'C:\wpkg\wpkg.bat') ->withCheck('uninstall', 'exists', 'WPKG 0.6-test1') ->withCommand('install', 'msiexec /i /qn "%SOFTWARE%\path\to\msi"', 'test', $pk2_exits) ->withCommand('remove', 'msiexec /x /qn "%SOFTWARE%\path\to\msi"') ->withCommand('upgrade', 'msiexec /i /qn "%SOFTWARE%\path\to\msi"') ->withCommand('downgrade', null, 'remove') ->withCommand('downgrade', null, 'install'); $_packages->setPackage($pk2); /** * Test package #3 */ $pk3 = new Package(); $pk3->with('id', 'time3') ->with('name', 'Time Synchronization') ->with('priority', 100) ->with('execute', 'always') ->withCheck('host', 'os', 'windows 7') ->withCommand('install', 'net time \\timeserver /set /yes'); $_packages->setPackage($pk3); echo $_packages->show();
结果
<?xml version="1.0" encoding="UTF-8"?> <packages:packages xmlns:packages="http://www.wpkg.org/packages" xmlns:wpkg="http://www.wpkg.org/wpkg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wpkg.org/packages xsd/packages.xsd"> <package id="time" name="Time Synchronization" priority="100" execute="always"> <check type="host" condition="os" path="windows 7"/> <commands> <command type="install" cmd="net time \timeserver /set /yes"/> </commands> </package> <package id="wpkg" name="Windows Packager sample 1" revision="1" priority="0" reboot="false"> <check type="registry" condition="exists" path="HKLM\Software\wpkg\full\key\not\part\of\it"/> <check type="file" condition="exists" path="C:\wpkg\wpkg.bat"/> <check type="uninstall" condition="exists" path="WPKG 0.6-test1"/> <commands> <command type="install" cmd="msiexec /i /qn "%SOFTWARE%\path\to\msi"" include="test"> <exit code="0"/> <exit code="3010" reboot="true"/> <exit code="any"/> <exit code="2"/> </command> <command type="remove" cmd="msiexec /x /qn "%SOFTWARE%\path\to\msi""/> <command type="upgrade" cmd="msiexec /i /qn "%SOFTWARE%\path\to\msi""/> <command type="downgrade" include="remove"/> <command type="downgrade" include="install"/> </commands> </package> <package id="time3" name="Time Synchronization" priority="100" execute="always"> <check type="host" condition="os" path="windows 7"/> <commands> <command type="install" cmd="net time \timeserver /set /yes"/> </commands> </package> </packages:packages>
如何导入现有XML
导入Config.xml文件
首先您需要启用导入类
use \WPKG\Drivers\XMLImport; // Create new object $_import = new XMLImport(); // Content of hosts file $_hosts_file = file_get_contents('config.xml'); // Read and parse file to normal format $_hosts = $_import->import($_hosts_file); // Print array to stdOut print_r($_hosts);
现在在 $_hosts
变量中,您可以找到包含所有已导入主机的 \WPKG\Hosts 对象。
对所有其他配置执行相同的操作,库可以检查您加载了哪个配置。
获取支持!
- Discord - 加入我们。
- GitHub Issues - 您有疑问?请告诉我们!
- Roadmap - 想要贡献力量?加入我们!
一些链接
- 主WPKG网站 - https://wpkg.org/
- WPKG 文档页面 - https://wpkg.org/Documentation
- 维基百科上的文章 - https://en.wikipedia.org/wiki/WPKG_(software)