techdivision /
import-cli
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * TechDivision\Import\Cli\Application |
||
| 5 | * |
||
| 6 | * PHP version 7 |
||
| 7 | * |
||
| 8 | * @author Tim Wagner <[email protected]> |
||
| 9 | * @copyright 2016 TechDivision GmbH <[email protected]> |
||
| 10 | * @license https://opensource.org/licenses/MIT |
||
| 11 | * @link https://github.com/techdivision/import-cli-simple |
||
| 12 | * @link http://www.techdivision.com |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace TechDivision\Import\Cli; |
||
| 16 | |||
| 17 | use TechDivision\Import\Cli\Utils\DependencyInjectionKeys; |
||
| 18 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
| 19 | use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The M2IF - Console Tool implementation. |
||
| 23 | * |
||
| 24 | * This is a example console tool implementation that should give developers an impression |
||
| 25 | * on how the M2IF could be used to implement their own Magento 2 importer. |
||
| 26 | * |
||
| 27 | * @author Tim Wagner <[email protected]> |
||
| 28 | * @copyright 2016 TechDivision GmbH <[email protected]> |
||
| 29 | * @license https://opensource.org/licenses/MIT |
||
| 30 | * @link https://github.com/techdivision/import-cli-simple |
||
| 31 | * @link http://www.techdivision.com |
||
| 32 | */ |
||
| 33 | class Application extends \Symfony\Component\Console\Application implements ContainerAwareInterface |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Regex to read the actual version number from the .semver file. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const REGEX = "/^\-\-\-\n:major:\s(0|[1-9]\d*)\n:minor:\s(0|[1-9]\d*)\n:patch:\s(0|[1-9]\d*)\n:special:\s'([a-zA-z0-9]*\.?(?:0|[1-9]\d*)?)'\n:metadata:\s'((?:0|[1-9]\d*)?(?:\.[a-zA-z0-9\.]*)?)'/"; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The DI container instance. |
||
| 45 | * |
||
| 46 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
||
| 47 | */ |
||
| 48 | protected $container; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The constructor to initialize the instance. |
||
| 52 | * |
||
| 53 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance |
||
| 54 | */ |
||
| 55 | 1 | public function __construct(ContainerInterface $container) |
|
| 56 | { |
||
| 57 | |||
| 58 | // set the DI container instance |
||
| 59 | 1 | $this->setContainer($container); |
|
| 60 | |||
| 61 | // initialize the variables for the elements of the version string |
||
| 62 | 1 | $major = 1; |
|
| 63 | 1 | $minor = 0; |
|
| 64 | 1 | $patch = 0; |
|
| 65 | 1 | $special = null; |
|
| 66 | 1 | $metadata = null; |
|
| 67 | |||
| 68 | // load the base directory (necessary to locate the base .semver file) |
||
| 69 | 1 | $baseDir = $container->getParameter(DependencyInjectionKeys::CONFIGURATION_BASE_DIR); |
|
| 70 | |||
| 71 | // parse the file with the semantic versioning data |
||
| 72 | 1 | extract($this->parse($baseDir . DIRECTORY_SEPARATOR . $container->getParameter(DependencyInjectionKeys::APPLICATION_VERSION_FILE))); |
|
| 73 | |||
| 74 | // invoke the parent constructor |
||
| 75 | 1 | parent::__construct( |
|
| 76 | 1 | $container->getParameter(DependencyInjectionKeys::APPLICATION_NAME), |
|
| 77 | 1 | sprintf('%d.%d.%d', $major, $minor, $patch) . ($special ? sprintf('-%s%s', $special, $metadata) : null) |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 78 | 1 | ); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sets the DI container instance. |
||
| 83 | * |
||
| 84 | * @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container The DI container instance |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | 1 | public function setContainer(ContainerInterface $container = null) |
|
| 89 | { |
||
| 90 | 1 | $this->container = $container; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Return's the DI container instance. |
||
| 95 | * |
||
| 96 | * @return \Symfony\Component\DependencyInjection\ContainerInterface|null The DI container instance |
||
| 97 | */ |
||
| 98 | 1 | public function getContainer() |
|
| 99 | { |
||
| 100 | 1 | return $this->container; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Parse and return the version number from the application's .semver file. |
||
| 105 | * |
||
| 106 | * @param string $semverFile The path to the semver file |
||
| 107 | * |
||
| 108 | * @return array The array with the version information |
||
| 109 | * @throws \Exception Is thrown, if the .semver file is not available or invalid |
||
| 110 | */ |
||
| 111 | 1 | protected function parse($semverFile) |
|
| 112 | { |
||
| 113 | |||
| 114 | // load the content of the semver file |
||
| 115 | 1 | $output = file_get_contents($semverFile); |
|
| 116 | |||
| 117 | // initialize the array with the matches |
||
| 118 | 1 | $matches = array(); |
|
| 119 | |||
| 120 | // extract the version information |
||
| 121 | 1 | if (!preg_match_all(self::REGEX, $output, $matches)) { |
|
| 122 | throw new \Exception('Bad semver file.'); |
||
| 123 | } |
||
| 124 | |||
| 125 | // prepare and return the version number |
||
| 126 | 1 | list(, $major, $minor, $patch, $special, $metadata) = array_map('current', $matches); |
|
| 127 | 1 | return compact('major', 'minor', 'patch', 'special', 'metadata'); |
|
| 128 | } |
||
| 129 | } |
||
| 130 |