예제 #1
0
    public function testAddFromString()
    {
        $original = <<<SOURCE
<?php

/**
 * My class.
 */
class @thing@
{
    /**
     * My method.
     */
    public function @other_thing@()
    {
    }
}
SOURCE;
        $expected = <<<SOURCE
<?php




class MyClass
{



public function myMethod()
{
}
}
SOURCE;
        $this->crate->addCompactor(new Php());
        $this->crate->setValues(array('@thing@' => 'MyClass', '@other_thing@' => 'myMethod'));
        $this->crate->addFromString('test/test.php', $original);
        $this->assertEquals($expected, file_get_contents('phar://test.phar/test/test.php'));
    }
예제 #2
0
파일: Build.php 프로젝트: cmpayments/crate
 /**
  * @override
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->config = $this->getConfig($input);
     $path = $this->config->getOutputPath();
     // load bootstrap file
     if (null !== ($bootstrap = $this->config->getBootstrapFile())) {
         $this->config->loadBootstrap();
         $this->putln('?', "Loading bootstrap file: {$bootstrap}");
         unset($bootstrap);
     }
     // remove any previous work
     if (file_exists($path)) {
         $this->putln('?', 'Removing previously built Phar...');
         unlink($path);
     }
     // set up Crate
     if ($this->isVerbose()) {
         $this->putln('*', 'Building...');
     } else {
         $output->writeln('Building...');
     }
     $this->putln('?', "Output path: {$path}");
     $this->crate = Crate::create($path);
     $this->crate->getPhar()->startBuffering();
     // set replacement values, if any
     if (array() !== ($values = $this->config->getProcessedReplacements())) {
         $this->putln('?', 'Setting replacement values...');
         if ($this->isVerbose()) {
             foreach ($values as $key => $value) {
                 $this->putln('+', "{$key}: {$value}");
             }
         }
         $this->crate->setValues($values);
         unset($values, $key, $value);
     }
     // register configured compactors
     if (array() !== ($compactors = $this->config->getCompactors())) {
         $this->putln('?', 'Registering compactors...');
         foreach ($compactors as $compactor) {
             $this->putln('+', get_class($compactor));
             $this->crate->addCompactor($compactor);
         }
     }
     // alert about mapped paths
     if (array() !== ($map = $this->config->getMap())) {
         $this->putln('?', 'Mapping paths:');
         foreach ($map as $item) {
             foreach ($item as $match => $replace) {
                 if (empty($match)) {
                     $match = "(all)";
                 }
                 $this->putln('-', "{$match} <info>></info> {$replace}");
             }
         }
     }
     // start adding files
     if (array() !== ($iterators = $this->config->getFinders())) {
         $this->putln('?', 'Adding Finder files...');
         foreach ($iterators as $iterator) {
             $this->add($iterator, null);
         }
     }
     if (array() !== ($iterators = $this->config->getBinaryFinders())) {
         $this->putln('?', 'Adding binary Finder files...');
         foreach ($iterators as $iterator) {
             $this->add($iterator, null, true);
         }
     }
     $this->add($this->config->getDirectoriesIterator(), 'Adding directories...');
     $this->add($this->config->getBinaryDirectoriesIterator(), 'Adding binary directories...', true);
     $this->add($this->config->getFilesIterator(), 'Adding files...');
     $this->add($this->config->getBinaryFilesIterator(), 'Adding binary files...', true);
     if (null !== ($main = $this->config->getMainScriptPath())) {
         $this->putln('?', 'Adding main file: ' . $this->config->getBasePath() . DIRECTORY_SEPARATOR . $main);
         $mapper = $this->config->getMapper();
         $pharPath = $mapper($main);
         if (null !== $pharPath) {
             $this->putln('>', $pharPath);
             $main = $pharPath;
         }
         $this->crate->addFromString($main, $this->config->getMainScriptContents());
     }
     // set the appropriate stub
     if (true === $this->config->isStubGenerated()) {
         $this->putln('?', 'Generating new stub...');
         $stub = StubGenerator::create()->alias($this->config->getAlias())->extract($this->config->isExtractable())->index($main)->intercept($this->config->isInterceptFileFuncs())->mimetypes($this->config->getMimetypeMapping())->mung($this->config->getMungVariables())->notFound($this->config->getNotFoundScriptPath())->web($this->config->isWebPhar());
         if (null !== ($shebang = $this->config->getShebang())) {
             $this->putln('-', 'Using custom shebang line: ' . $shebang);
             $stub->shebang($shebang);
         }
         if (null !== ($lsbInitParams = $this->config->getLsbInitParams())) {
             foreach ($lsbInitParams as $param => $value) {
                 $stub->lsbInitParam($param, $value);
             }
             unset($param, $value);
         }
         if (null !== ($banner = $this->config->getStubBanner())) {
             $this->putln('-', 'Using custom banner.');
             $stub->banner($banner);
         } elseif (null !== ($banner = $this->config->getStubBannerFromFile())) {
             $this->putln('-', 'Using custom banner from file: ' . $this->config->getBasePath() . DIRECTORY_SEPARATOR . $this->config->getStubBannerPath());
             $stub->banner($banner);
         }
         $this->crate->getPhar()->setStub($stub->generate());
     } elseif (null !== ($stub = $this->config->getStubPath())) {
         $stub = $this->config->getBasePath() . DIRECTORY_SEPARATOR . $stub;
         $this->putln('?', "Using stub file: {$stub}");
         $this->crate->setStubUsingFile($stub);
     } else {
         $this->putln('?', 'Using default stub.');
     }
     // set metadata, if any
     if (null !== ($metadata = $this->config->getMetadata())) {
         $this->putln('?', 'Setting metadata...');
         $this->crate->getPhar()->setMetadata($metadata);
     }
     // compress, if algorithm set
     if (null !== ($algorithm = $this->config->getCompressionAlgorithm())) {
         $this->putln('?', 'Compressing...');
         $this->crate->getPhar()->compressFiles($algorithm);
     }
     $this->crate->getPhar()->stopBuffering();
     // sign using private key, if applicable
     if (file_exists($path . '.pubkey')) {
         unlink($path . '.pubkey');
     }
     if (null !== ($key = $this->config->getPrivateKeyPath())) {
         $this->putln('?', 'Signing using a private key...');
         $passphrase = $this->config->getPrivateKeyPassphrase();
         if ($this->config->isPrivateKeyPrompt()) {
             /** @var $dialog DialogHelper */
             $dialog = $this->getHelper('dialog');
             $passphrase = $dialog->askHiddenResponse($output, 'Private key passphrase:');
         }
         $this->crate->signUsingFile($key, $passphrase);
         // set the signature algorithm if no key is used
     } elseif (null !== ($algorithm = $this->config->getSigningAlgorithm())) {
         $this->crate->getPhar()->setSignatureAlgorithm($algorithm);
     }
     unset($this->crate);
     // chmod, if configured
     if (null !== ($chmod = $this->config->getFileMode())) {
         $this->putln('?', 'Setting file permissions...');
         chmod($path, $chmod);
     }
     $this->putln('*', 'Done.');
     if (!file_exists($path)) {
         $output->writeln('<fg=yellow>The archive was not generated because it did not have any contents.</fg=yellow>');
     }
 }
예제 #3
0
파일: Add.php 프로젝트: cmpayments/crate
 /**
  * @override
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (false === $input->getOption('stub') && null === $input->getArgument('local')) {
         $output->writeln('<error>The <info>local</info> argument is required.</error>');
         return 1;
     }
     $this->config = $this->getConfig($input);
     $phar = $input->getArgument('phar');
     $file = $input->getArgument('file');
     // load bootstrap file
     if (null !== ($bootstrap = $this->config->getBootstrapFile())) {
         $this->config->loadBootstrap();
         $this->putln('?', "Loading bootstrap file: {$bootstrap}");
         unset($bootstrap);
     }
     $this->putln('*', 'Adding to the Phar...');
     if (false === is_file($phar)) {
         $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $phar));
         return 1;
     }
     if (false === is_file($file)) {
         $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $file));
         return 1;
     }
     if (false == preg_match('/^\\w+:\\/\\//', $file)) {
         $file = realpath($file);
     }
     $this->crate = Crate::create($phar);
     // set replacement values, if any
     if (array() !== ($values = $this->config->getProcessedReplacements())) {
         $this->putln('?', 'Setting replacement values...');
         if ($this->isVerbose()) {
             foreach ($values as $key => $value) {
                 $this->putln('+', "{$key}: {$value}");
             }
         }
         $this->crate->setValues($values);
         unset($values, $key, $value);
     }
     // register configured compactors
     if (array() !== ($compactors = $this->config->getCompactors())) {
         $this->putln('?', 'Registering compactors...');
         foreach ($compactors as $compactor) {
             $this->putln('+', get_class($compactor));
             $this->crate->addCompactor($compactor);
         }
     }
     // add the file
     $phar = $this->crate->getPhar();
     $local = str_replace('\\', '/', Path::canonical($input->getArgument('local')));
     if (isset($phar[$local]) && false === $input->getOption('replace')) {
         $output->writeln(sprintf('<error>The file "%s" already exists in the Phar.</error>', $local));
         return 1;
     }
     if ($input->getOption('binary')) {
         $this->putln('?', "Adding binary file: {$file}");
         $phar->addFile($file, $local);
     } elseif ($input->getOption('stub')) {
         $this->putln('?', "Using stub file: {$file}");
         $this->crate->setStubUsingFile($file);
     } elseif ($input->getOption('main')) {
         $this->putln('?', "Adding main file: {$file}");
         if (false === ($contents = @file_get_contents($file))) {
             $error = error_get_last();
             throw new RuntimeException($error['message']);
         }
         $this->crate->addFromString($local, preg_replace('/^#!.*\\s*/', '', $contents));
     } else {
         $this->putln('?', "Adding file: {$file}");
         $this->crate->addFile($file, $local);
     }
     unset($this->crate, $phar);
     $this->putln('*', 'Done.');
     return 0;
 }