/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = $this->codeWriter->insertMethodFirstInClass($code, $content); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->buildArgumentString($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('interface-method-signature', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $this->insertMethodSignature($filepath, $content); $this->io->writeln(sprintf("<info>Method signature <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = count($arguments) ? '$argument' . implode(', $argument', range(1, count($arguments))) : ''; $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $this->filesystem->putFileContents($filepath, $this->getUpdatedCode($name, $content, $code)); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $method = $data['method']; $expected = $data['expected']; $code = $this->filesystem->getFileContents($resource->getSrcFilename()); $values = array('%constant%' => var_export($expected, true)); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $pattern = '/' . '(function\\s+' . preg_quote($method, '/') . '\\s*\\([^\\)]*\\))\\s+{[^}]*?}/'; $replacement = '$1' . $content; $modifiedCode = preg_replace($pattern, $replacement, $code); $this->filesystem->putFileContents($resource->getSrcFilename(), $modifiedCode); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been modified.</info>", $resource->getSrcClassname(), $method), 2); }
/** * @param Resource $resource * @param array $data * * @return mixed */ public function generate(Resource $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->argumentBuilder->buildFrom($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been created.</info>", $resource->getSrcClassname(), $name), 2); }
/** * @param ServiceContainer $container */ private function setupGenerators(ServiceContainer $container) { $container->setShared('code_generator', function (ServiceContainer $c) { $generator = new CodeGenerator\GeneratorManager(); array_map(array($generator, 'registerGenerator'), $c->getByPrefix('code_generator.generators')); return $generator; }); $container->set('code_generator.generators.specification', function (ServiceContainer $c) { $specificationGenerator = new CodeGenerator\Generator\SpecificationGenerator($c->get('console.io'), $c->get('code_generator.templates')); return new CodeGenerator\Generator\NewFileNotifyingGenerator($specificationGenerator, $c->get('event_dispatcher')); }); $container->set('code_generator.generators.class', function (ServiceContainer $c) { $classGenerator = new CodeGenerator\Generator\ClassGenerator($c->get('console.io'), $c->get('code_generator.templates'), null, $c->get('process.executioncontext')); return new CodeGenerator\Generator\NewFileNotifyingGenerator($classGenerator, $c->get('event_dispatcher')); }); $container->set('code_generator.generators.interface', function (ServiceContainer $c) { $interfaceGenerator = new CodeGenerator\Generator\InterfaceGenerator($c->get('console.io'), $c->get('code_generator.templates'), null, $c->get('process.executioncontext')); return new CodeGenerator\Generator\NewFileNotifyingGenerator($interfaceGenerator, $c->get('event_dispatcher')); }); $container->set('code_generator.generators.method', function (ServiceContainer $c) { return new CodeGenerator\Generator\MethodGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->set('code_generator.generators.methodSignature', function (ServiceContainer $c) { return new CodeGenerator\Generator\MethodSignatureGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->set('code_generator.generators.returnConstant', function (ServiceContainer $c) { return new CodeGenerator\Generator\ReturnConstantGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->set('code_generator.generators.named_constructor', function (ServiceContainer $c) { return new CodeGenerator\Generator\NamedConstructorGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->set('code_generator.generators.private_constructor', function (ServiceContainer $c) { return new CodeGenerator\Generator\PrivateConstructorGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->setShared('code_generator.templates', function (ServiceContainer $c) { $renderer = new CodeGenerator\TemplateRenderer(); $renderer->setLocations($c->getParam('code_generator.templates.paths', array())); return $renderer; }); if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; } else { $home = getenv('HOME'); } $container->setParam('code_generator.templates.paths', array(rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.phpspec', rtrim($home, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.phpspec')); }
/** * @param ServiceContainer $container */ protected function setupGenerators(ServiceContainer $container) { $container->setShared('code_generator', function ($c) { $generator = new CodeGenerator\GeneratorManager(); array_map(array($generator, 'registerGenerator'), $c->getByPrefix('code_generator.generators')); return $generator; }); $container->set('code_generator.generators.specification', function ($c) { return new CodeGenerator\Generator\SpecificationGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->set('code_generator.generators.class', function ($c) { return new CodeGenerator\Generator\ClassGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->set('code_generator.generators.method', function ($c) { return new CodeGenerator\Generator\MethodGenerator($c->get('console.io'), $c->get('code_generator.templates')); }); $container->setShared('code_generator.templates', function ($c) { $renderer = new CodeGenerator\TemplateRenderer(); $renderer->setLocations($c->getParam('code_generator.templates.paths', array())); return $renderer; }); if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; } else { $home = $_SERVER['HOME']; } $container->setParam('code_generator.templates.paths', array(rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.phpspec', rtrim($home, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.phpspec')); }