protected function setupDatabase(InputInterface $input, OutputInterface $output)
 {
     if ($this->manager === NULL) {
         $questionHelper = $this->getHelper('question');
         $file = Filesystem::normalizePath($this->configFile);
         if (!is_file($file)) {
             $output->writeln(sprintf('Missing config file: <info>%s</info>', $this->configFile));
             $question = new ConfirmationQuestion('Do you want to generate the config file? [n] ', false);
             if ($questionHelper->ask($input, $output, $question)) {
                 $tpl = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'ConfigTemplate.txt');
                 $replacements = [];
                 $question = new Question('Database DSN (PDO): ', '');
                 $dsn = $questionHelper->ask($input, $output, $question);
                 $question = new Question('DB username: '******'');
                 $username = $questionHelper->ask($input, $output, $question);
                 $question = new Question('DB password: '******'');
                 $password = $questionHelper->ask($input, $output, $question);
                 $replacements['###DSN###'] = var_export((string) $dsn, true);
                 $replacements['###USERNAME###'] = var_export(trim($username) === '' ? NULL : $username, true);
                 $replacements['###PASSWORD###'] = var_export(trim($password) === '' ? NULL : $password, true);
                 $code = strtr($tpl, $replacements);
                 Filesystem::writeFile($file, $code);
                 $output->writeln(sprintf('Generated <info>%s</file> with this contents:', $file));
                 $output->writeln('');
                 $output->writeln($code);
             }
             return false;
         }
         $config = new Configuration($this->processConfigData(require $file));
         $this->manager = new ConnectionManager($config->getConfig('ConnectionManager'));
         $this->migrationDirectories = $config->getConfig('Migration.MigrationManager.directories')->toArray();
     }
     return true;
 }
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $file = $input->getArgument('file');
     if (!preg_match("'^/|(?:[^:\\\\/]+://)|(?:[a-z]:[\\\\/])'i", $file)) {
         $file = $this->directory . '/resource/' . $file . '.html.xml';
     }
     $file = Filesystem::normalizePath($file);
     $questionHelper = $this->getHelper('question');
     $question = new ConfirmationQuestion(sprintf('Create view <info>%s</info>? [n] ', $file), false);
     if (!$questionHelper->ask($input, $output, $question)) {
         return;
     }
     $xml = new \DOMDocument('1.0', 'UTF-8');
     $xml->formatOutput = true;
     $root = $xml->appendChild($xml->createElementNS(ExpressViewParser::NS_EXPRESS, 'k:composition'));
     $root->appendChild($xml->createAttribute('extends'));
     $root->appendChild($xml->createAttribute('xmlns'))->appendChild($xml->createTextNode(ExpressViewParser::NS_XHTML));
     $root->appendChild($xml->createTextNode("\n\n  "));
     $block = $root->appendChild($xml->createElementNS(ExpressViewParser::NS_EXPRESS, 'k:block'));
     $block->appendChild($xml->createAttribute('name'))->appendChild($xml->createTextNode('main'));
     $block->appendChild($xml->createTextNode("\n    TODO: Create composition contents...\n  "));
     $root->appendChild($xml->createTextNode("\n\n"));
     Filesystem::writeFile($file, $xml->saveXML());
     $output->writeln('');
     $output->writeln(sprintf('CREATED: <info>%s</info>', $file));
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function generate($name, array $params = [], $style = self::ABSOLUTE_URI)
 {
     if ('/' !== substr($name, 0, 1)) {
         $match = $this->context->getRouteMatch();
         if ($match !== NULL) {
             $name = rtrim('/' . implode('/', explode('/', $match->getRouteName())), '/') . '/' . $name;
         }
     }
     $name = Filesystem::normalizePath('/' . trim($name, '/'));
     if ($name === '/') {
         switch ($style) {
             case self::ABSOLUTE_PATH:
                 return new UriInfo(rtrim('/' . $this->context->getRequest()->getPathBase(), '/') . '/');
             case self::ABSOLUTE_URI:
                 return new UriInfo(rtrim($this->context->getRequest()->getBaseUri(), '/') . '/');
             case self::NETWORK_PATH:
                 return new UriInfo(rtrim($this->context->getRequest()->getBaseUri()->setScheme(NULL), '/') . '/');
         }
         throw new \InvalidArgumentException(sprintf('Invalid URI generation style: "%s"', $style));
     }
     // Iterative route lookup:
     $router = $this->router;
     $info = NULL;
     foreach (explode('/', trim($name, '/')) as $step) {
         if ($info === NULL) {
             $info = $router->getRoute($step);
         } else {
             $info = $info->append($router->getRoute($step));
         }
         $handler = $info->getHandler();
         if ($handler instanceof DispatchableMountHandler) {
             break;
         } elseif ($handler instanceof MountHandler) {
             if (NULL !== ($router = $handler->getRouter())) {
                 continue;
             }
             $router = $this->dispatcher->publishUntil(new LoadMountedRouterEvent($handler), function ($result) {
                 return $result instanceof Router;
             });
             if ($router === NULL) {
                 throw new \RuntimeException(sprintf('No router could be loaded for mount %s', get_class($handler)));
             }
             $handler->setRouter($router);
         }
     }
     $info = $this->resolveParams($info, $params);
     if ($style == self::ABSOLUTE_PATH) {
         return $info->prepend(rtrim('/' . $this->context->getRequest()->getPathBase(), '/'));
     }
     if ($style == self::ABSOLUTE_URI) {
         return $info->prepend(rtrim($this->context->getRequest()->getBaseUri(), '/'));
     }
     if ($style == self::NETWORK_PATH) {
         return $info->prepend(rtrim($this->context->getRequest()->getBaseUri()->setScheme(NULL), '/'));
     }
     throw new \InvalidArgumentException(sprintf('Invalid URI generation style: "%s"', $style));
 }
Esempio n. 4
0
 public function getResource($resource)
 {
     $dir = Filesystem::normalizePath($this->directory);
     if ($resource === NULL || $resource === '') {
         return $dir . '/resource';
     }
     $entry = Filesystem::normalizePath($this->directory . '/resource/' . ltrim($resource, '/\\'));
     if (!file_exists($entry)) {
         throw new \RuntimeException(sprintf('Resource "%s" not found in komponent "%s"', $resource, $this->getKey()));
     }
     if ($entry == $dir) {
         return $entry;
     }
     if (0 !== strpos($entry, $dir . '/')) {
         throw new \RuntimeException(sprintf('Resource "%s" is not accessible by komponent "%s"', $resource, $this->getKey()));
     }
     return $entry;
 }
Esempio n. 5
0
 /**
  * Check if the HTTP request matches a public file and server it as needed.
  * 
  * @param HttpRequest $request
  * @param NextMiddleware $next
  * @return HttpResponse
  */
 public function __invoke(HttpRequest $request, NextMiddleware $next) : \Generator
 {
     static $methods = [Http::HEAD, Http::GET];
     if (!\in_array($request->getMethod(), $methods, true)) {
         return yield from $next($request);
     }
     $path = '/' . \trim($request->getRequestTarget(), '/');
     if ($this->basePath !== '/') {
         if (0 !== \strpos($path, $this->basePath)) {
             return yield from $next($request);
         }
         $path = \substr($path, \strlen($this->basePath) - 1);
     }
     $file = Filesystem::normalizePath($this->directory . \substr($path, 1));
     if (0 !== \strpos($file, $this->directory)) {
         return yield from $next($request);
     }
     if (!(yield LoopConfig::currentFilesystem()->isFile($file))) {
         return yield from $next($request);
     }
     return $this->createResponse($request, $file);
 }
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $renderers = $this->view->getRenderers(function ($renderer) {
         return $renderer instanceof ExpressViewRenderer;
     });
     if (empty($renderers)) {
         throw new \RuntimeException(sprintf('Express view renderer not found'));
     }
     $dir = $input->getOption('dir');
     if ($dir === NULL) {
         $dir = 'resource/schema';
     }
     if (!preg_match("'^/|(?:[^:\\\\/]+://)|(?:[a-z]:[\\\\/])'i", $dir)) {
         $dir = $this->directory . '/' . $dir;
     }
     $dir = Filesystem::normalizePath($dir);
     if (!is_dir($dir)) {
         $questionHelper = $this->getHelper('question');
         $question = new ConfirmationQuestion(sprintf('Schema directory <info>%s</info> does not exist, dou you want to create it? [n] ', $dir), false);
         if (!$questionHelper->ask($input, $output, $question)) {
             return;
         }
         Filesystem::createDirectory($dir);
     }
     $renderer = array_pop($renderers);
     $namespace = $input->getArgument('namespace');
     $catalogFile = $dir . '/catalog.xml';
     $xml = new \DOMDocument('1.0', 'UTF-8');
     $xml->preserveWhiteSpace = false;
     $xml->formatOutput = true;
     if (is_file($catalogFile)) {
         $xml->load($catalogFile);
         $catalogElement = $xml->documentElement;
     } else {
         $catalogElement = $xml->appendChild($xml->createElementNS(self::NS_CATALOG, 'c:catalog'));
         $catalogElement->appendChild($xml->createAttribute('prefer'))->appendChild($xml->createTextNode('public'));
     }
     $xpath = new \DOMXPath($xml);
     $xpath->registerNamespace('c', self::NS_CATALOG);
     $count = 0;
     $skipped = 0;
     foreach ($renderer->generateXmlSchemaBuilders() as $builder) {
         if (!$builder instanceof HelperXmlSchemaBuilder) {
             continue;
         }
         $ns = $builder->getNamespace();
         if ($namespace != $ns) {
             continue;
         }
         $code = (string) $builder;
         $file = sprintf('%s/%s.xsd', $dir, str_replace(':', '-', $ns));
         if (!is_file($file) || md5_file($file) !== md5($code)) {
             $count++;
             Filesystem::writeFile($file, $code);
             $output->writeln('');
             $output->writeln(sprintf('<info>%s</info>', basename($file)));
             $output->writeln(sprintf('  Generated XML-Schema for namespace <comment>%s</comment>.', $ns));
         } else {
             $skipped++;
         }
         $found = false;
         foreach ($xpath->query("/c:catalog/c:uri[@name='" . $ns . "']") as $el) {
             $found = $el ? true : false;
         }
         if (!$found) {
             $uriElement = $catalogElement->appendChild($xml->createElementNS(self::NS_CATALOG, 'c:uri'));
             $uriElement->appendChild($xml->createAttribute('name'))->appendChild($xml->createTextNode($ns));
             $uriElement->appendChild($xml->createAttribute('uri'))->appendChild($xml->createTextNode(str_replace(':', '-', $ns) . '.xsd'));
         }
     }
     $output->writeln('');
     $output->writeln(sprintf('Created <info>%u</info> schema files in directory <comment>%s</comment>', $count, $dir));
     Filesystem::writeFile($catalogFile, $xml->saveXML());
 }
Esempio n. 7
0
 public static function locateFileSync(string $path, array $paths) : string
 {
     if ('k1://' === \substr($path, 0, 5)) {
         $normalized = \substr($path, 5);
     } else {
         $normalized = $path;
         $path = 'k1://' . $path;
     }
     $normalized = Filesystem::normalizePath($normalized);
     if (0 === \strpos($normalized, 'app/')) {
         $package = 'app';
         $sub = \substr($normalized, 4);
     } else {
         $m = null;
         if (!\preg_match("'^([^/\\.]+/[^/\\.]+)/(.+)\$'", $normalized, $m)) {
             throw new ResourceNotFoundException(\sprintf('Invalid resource location: "%s" (%s)', $path, $normalized));
         }
         $package = $m[1];
         $sub = $m[2];
     }
     if (empty($paths[$package])) {
         throw new ResourceNotFoundException(\sprintf('Provider not loaded: "%s"', $package));
     }
     foreach ($paths[$package] as $base) {
         $file = $base . '/' . $sub;
         if (\is_file($file)) {
             return $file;
         }
     }
     throw new ResourceNotFoundException(\sprintf('Resource not found: "%s"', $path));
 }
Esempio n. 8
0
 /**
  * Create a new unit config.
  * 
  * @param string $file Config file.
  * @param int $priority Loading priority.
  * @param string $unitName Unit name (should be identical to the root namespace of your package).
  */
 public function __construct(string $file, int $priority = 0, string $unitName = '')
 {
     $this->file = Filesystem::normalizePath($file);
     $this->priority = $priority;
     $this->unitName = $unitName;
 }
Esempio n. 9
0
 /**
  * Create a new unit that loads DI container object configuration from the given file.
  * 
  * @param string $file DI container object config file.
  * @param int $priority Loading priority.
  */
 public function __construct(string $file, int $priority = 0)
 {
     $this->file = Filesystem::normalizePath($file);
     $this->priority = $priority;
 }