mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Add plugin info retrieval
This commit is contained in:
parent
c1cd718ecc
commit
2236b3cfbe
7 changed files with 816 additions and 245 deletions
134
src/FilePathPluginFactory.php
Normal file
134
src/FilePathPluginFactory.php
Normal file
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
/**
|
||||
* Extracts plugin info from plugin file path.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce
|
||||
*
|
||||
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag
|
||||
* @phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce;
|
||||
|
||||
use Dhii\Package\Version\StringVersionFactoryInterface;
|
||||
use Dhii\Package\Version\VersionInterface;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
use UnexpectedValueException;
|
||||
use WpOop\WordPress\Plugin\FilePathPluginFactoryInterface;
|
||||
use WpOop\WordPress\Plugin\PluginInterface;
|
||||
|
||||
/**
|
||||
* Extracts plugin info from plugin file path.
|
||||
*/
|
||||
class FilePathPluginFactory implements FilePathPluginFactoryInterface {
|
||||
|
||||
/**
|
||||
* The version factory.
|
||||
*
|
||||
* @var StringVersionFactoryInterface
|
||||
*/
|
||||
protected $version_factory;
|
||||
|
||||
/**
|
||||
* FilePathPluginFactory constructor.
|
||||
*
|
||||
* @param StringVersionFactoryInterface $version_factory The version factory.
|
||||
*/
|
||||
public function __construct( StringVersionFactoryInterface $version_factory ) {
|
||||
$this->version_factory = $version_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts plugin info from plugin file path.
|
||||
*
|
||||
* @param string $filePath The plugin file path.
|
||||
*/
|
||||
public function createPluginFromFilePath( string $filePath ): PluginInterface {
|
||||
if ( ! is_readable( $filePath ) ) {
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
'Plugin file "%1$s" does not exist or is not readable',
|
||||
$filePath
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$plugin_data = get_plugin_data( $filePath );
|
||||
if ( empty( $plugin_data ) ) {
|
||||
throw new UnexpectedValueException(
|
||||
sprintf(
|
||||
'Plugin file "%1$s" does not have a valid plugin header',
|
||||
$filePath
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$plugin_data = array_merge(
|
||||
array(
|
||||
'Name' => '',
|
||||
'Version' => '0.1.0-alpha1+default',
|
||||
'Title' => '',
|
||||
'Description' => '',
|
||||
'TextDomain' => '',
|
||||
'RequiresWP' => '5.0',
|
||||
'RequiresPHP' => '7.1',
|
||||
),
|
||||
$plugin_data
|
||||
);
|
||||
|
||||
$base_dir = dirname( $filePath );
|
||||
$base_name = plugin_basename( $filePath );
|
||||
$slug = $this->get_plugin_slug( $base_name );
|
||||
$text_domain = ! empty( $plugin_data['TextDomain'] ) ? $plugin_data['TextDomain'] : $slug;
|
||||
|
||||
return new Plugin(
|
||||
$plugin_data['Name'],
|
||||
$this->create_version( $plugin_data['Version'] ),
|
||||
$base_dir,
|
||||
$base_name,
|
||||
$plugin_data['Title'],
|
||||
$plugin_data['Description'],
|
||||
$text_domain,
|
||||
$this->create_version( $plugin_data['RequiresPHP'] ),
|
||||
$this->create_version( $plugin_data['RequiresWP'] )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new version from a version string.
|
||||
*
|
||||
* @param string $version_string The SemVer-compliant version string.
|
||||
*
|
||||
* @return VersionInterface The new version.
|
||||
*
|
||||
* @throws Exception If version string is malformed.
|
||||
*/
|
||||
protected function create_version( string $version_string ): VersionInterface {
|
||||
return $this->version_factory->createVersionFromString( $version_string );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a plugin slug from its basename.
|
||||
*
|
||||
* @param string $base_name The plugin's basename.
|
||||
*
|
||||
* @return string The plugin's slug.
|
||||
*/
|
||||
protected function get_plugin_slug( string $base_name ): string {
|
||||
$directory_separator = '/';
|
||||
|
||||
// If plugin is in a directory, use directory name.
|
||||
if ( strstr( $base_name, $directory_separator ) !== false ) {
|
||||
$parts = explode( $directory_separator, $base_name );
|
||||
if ( $parts ) {
|
||||
return $parts[0];
|
||||
}
|
||||
}
|
||||
|
||||
// If plugin is not in a directory, return plugin file basename.
|
||||
return basename( $base_name );
|
||||
}
|
||||
}
|
180
src/Plugin.php
Normal file
180
src/Plugin.php
Normal file
|
@ -0,0 +1,180 @@
|
|||
<?php
|
||||
/**
|
||||
* Plugin properties.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce;
|
||||
|
||||
use Dhii\Package\Version\VersionInterface;
|
||||
use WpOop\WordPress\Plugin\PluginInterface;
|
||||
|
||||
/**
|
||||
* Plugin properties.
|
||||
*/
|
||||
class Plugin implements PluginInterface {
|
||||
|
||||
/**
|
||||
* The plugin name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* The plugin version.
|
||||
*
|
||||
* @var VersionInterface
|
||||
*/
|
||||
protected $version;
|
||||
|
||||
/**
|
||||
* The path to the plugin base directory.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $base_dir;
|
||||
|
||||
/**
|
||||
* The plugin base name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $base_name;
|
||||
|
||||
/**
|
||||
* The plugin title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* The plugin description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* The text domain of this plugin
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $text_domain;
|
||||
|
||||
/**
|
||||
* The minimal version of PHP required by this plugin.
|
||||
*
|
||||
* @var VersionInterface
|
||||
*/
|
||||
protected $min_php_version;
|
||||
|
||||
/**
|
||||
* The minimal version of WP required by this plugin.
|
||||
*
|
||||
* @var VersionInterface
|
||||
*/
|
||||
protected $min_wp_version;
|
||||
|
||||
/**
|
||||
* Plugin constructor.
|
||||
*
|
||||
* @param string $name The plugin name.
|
||||
* @param VersionInterface $version The plugin version.
|
||||
* @param string $base_dir The path to the plugin base directory.
|
||||
* @param string $base_name The plugin base name.
|
||||
* @param string $title The plugin title.
|
||||
* @param string $description The plugin description.
|
||||
* @param string $text_domain The text domain of this plugin.
|
||||
* @param VersionInterface $min_php_version The minimal version of PHP required by this plugin.
|
||||
* @param VersionInterface $min_wp_version The minimal version of WP required by this plugin.
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
VersionInterface $version,
|
||||
string $base_dir,
|
||||
string $base_name,
|
||||
string $title,
|
||||
string $description,
|
||||
string $text_domain,
|
||||
VersionInterface $min_php_version,
|
||||
VersionInterface $min_wp_version
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->description = $description;
|
||||
$this->version = $version;
|
||||
$this->base_dir = $base_dir;
|
||||
$this->base_name = $base_name;
|
||||
$this->title = $title;
|
||||
$this->text_domain = $text_domain;
|
||||
$this->min_php_version = $min_php_version;
|
||||
$this->min_wp_version = $min_wp_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin name.
|
||||
*/
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin description.
|
||||
*/
|
||||
public function getDescription(): string {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin version.
|
||||
*/
|
||||
public function getVersion(): VersionInterface {
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* The path to the plugin base directory.
|
||||
*/
|
||||
public function getBaseDir(): string {
|
||||
return $this->base_dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin base name.
|
||||
*/
|
||||
public function getBaseName(): string {
|
||||
return $this->base_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* The text domain of this plugin.
|
||||
*/
|
||||
public function getTextDomain(): string {
|
||||
return $this->text_domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin title.
|
||||
*/
|
||||
public function getTitle(): string {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* The minimal version of PHP required by this plugin.
|
||||
*/
|
||||
public function getMinPhpVersion(): VersionInterface {
|
||||
return $this->min_php_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* The minimal version of WP required by this plugin.
|
||||
*/
|
||||
public function getMinWpVersion(): VersionInterface {
|
||||
return $this->min_wp_version;
|
||||
}
|
||||
}
|
|
@ -23,7 +23,10 @@ class PluginModule implements ModuleInterface {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider( array(), array() );
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/services.php',
|
||||
require __DIR__ . '/extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
12
src/extensions.php
Normal file
12
src/extensions.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* The plugin module extensions.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce;
|
||||
|
||||
return array();
|
21
src/services.php
Normal file
21
src/services.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
/**
|
||||
* The plugin module services.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce;
|
||||
|
||||
use Dhii\Versions\StringVersionFactory;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use WpOop\WordPress\Plugin\PluginInterface;
|
||||
|
||||
return array(
|
||||
'ppcp.plugin' => function( ContainerInterface $container ) : PluginInterface {
|
||||
$factory = new FilePathPluginFactory( new StringVersionFactory() );
|
||||
return $factory->createPluginFromFilePath( dirname( realpath( __FILE__ ), 2 ) . '/woocommerce-paypal-payments.php' );
|
||||
},
|
||||
);
|
Loading…
Add table
Add a link
Reference in a new issue