AppserverIo\Appserver\Core\Utilities\AppEnvironmentHelper::getEnvironmentAwareGlobPattern PHP Метод

getEnvironmentAwareGlobPattern() публичный статический Метод

E.g. AppEnvironmentHelper::getEnvironmentAwareFilePath('webapps/example', 'META-INF/*-ds') => 'webapps/example/META-INF/*-ds.dev.xml'
public static getEnvironmentAwareGlobPattern ( string $appBase, string $fileGlob, integer $flags, string $fileExtension = 'xml' ) : string
$appBase string The base file path to the application
$fileGlob string The intermediate path (or glob pattern) from app base path to file extension
$flags integer The flags passed to the glob function
$fileExtension string The extension of the file, will default to 'xml'
Результат string
    public static function getEnvironmentAwareGlobPattern($appBase, $fileGlob, $flags = 0, $fileExtension = 'xml')
    {
        // get the file path modifier
        $modifier = static::getEnvironmentModifier($appBase);
        // as we default to a not modified path we have to be careful about the "two dots" schema .$modifier.$extension
        $defaultFilePath = $appBase . DIRECTORY_SEPARATOR . $fileGlob . '.' . $fileExtension;
        if (empty($modifier)) {
            // if we do not have a modifier we do not need to act upon anything, so we return the default
            return $defaultFilePath;
        } else {
            // we got a modifier we have to check if there is something reachable under the modified path, if not we will also return the default
            $modifiedPath = $appBase . DIRECTORY_SEPARATOR . $fileGlob . '.' . $modifier . '.' . $fileExtension;
            $potentialFiles = static::globDir($modifiedPath, $flags);
            if (!empty($potentialFiles)) {
                return $modifiedPath;
            }
            return $defaultFilePath;
        }
    }

Usage Example

 /**
  * Provisions all web applications.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
  *
  * @return void
  */
 public function provision(ApplicationInterface $application)
 {
     // check if the webapps directory exists
     if (is_dir($webappPath = $application->getWebappPath())) {
         // prepare the glob expression with the application's directories to parse
         $applicationDirectories = AppEnvironmentHelper::getEnvironmentAwareGlobPattern($webappPath, '{WEB-INF,META-INF}/provision', GLOB_BRACE);
         // load the service instance
         /** @var \AppserverIo\Appserver\Core\Api\ProvisioningService $service */
         $service = $this->getService();
         // load the configuration service instance
         /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
         $configurationService = $this->getInitialContext()->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService');
         // load the container node to initialize the system properties
         /** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
         $containerNode = $application->getContainer()->getContainerNode();
         // iterate through all provisioning files (provision.xml), validate them and attach them to the configuration
         foreach ($service->globDir($applicationDirectories, GLOB_BRACE) as $provisionFile) {
             try {
                 // validate the file, but skip it if the validation fails
                 $configurationService->validateFile($provisionFile, null);
                 // load the system properties
                 $properties = $service->getSystemProperties($containerNode);
                 // append the application specific properties
                 $properties->add(SystemPropertyKeys::WEBAPP, $webappPath);
                 $properties->add(SystemPropertyKeys::WEBAPP_NAME, basename($webappPath));
                 $properties->add(SystemPropertyKeys::WEBAPP_CACHE, $application->getCacheDir());
                 $properties->add(SystemPropertyKeys::WEBAPP_SESSION, $application->getSessionDir());
                 // create a new provision node instance and replace the properties
                 $provisionNode = new ProvisionNode();
                 $provisionNode->initFromFile($provisionFile);
                 $provisionNode->replaceProperties($properties);
                 // query whether we've a datasource configured or not
                 if ($datasource = $provisionNode->getDatasource()) {
                     // try to load the datasource from the system configuration
                     $datasourceNode = $service->findByName($datasource->getName());
                     // try to inject the datasource node if available
                     if ($datasourceNode != null) {
                         $provisionNode->injectDatasource($datasourceNode);
                     }
                 }
                 /* Re-provision the provision.xml (reinitialize).
                  *
                  * ATTENTION: The re-provisioning is extremely important, because
                  * this allows dynamic replacement of placeholders by using the
                  * XML file as a template that will reinterpreted with the PHP
                  * interpreter!
                  */
                 $provisionNode->reprovision($provisionFile);
                 // execute the provisioning workflow
                 $this->executeProvision($application, $provisionNode, new \SplFileInfo($webappPath));
             } catch (ConfigurationException $ce) {
                 // load the logger and log the XML validation errors
                 $systemLogger = $this->getInitialContext()->getSystemLogger();
                 $systemLogger->error($ce->__toString());
                 // additionally log a message that DS will be missing
                 $systemLogger->critical(sprintf('Will skip reading provisioning steps in %s, provisioning might not have been done.', $provisionFile));
             }
         }
     }
 }
All Usage Examples Of AppserverIo\Appserver\Core\Utilities\AppEnvironmentHelper::getEnvironmentAwareGlobPattern