本文整理汇总了PHP中Zend\Code\Generator\PropertyGenerator::setStatic方法的典型用法代码示例。如果您正苦于以下问题:PHP PropertyGenerator::setStatic方法的具体用法?PHP PropertyGenerator::setStatic怎么用?PHP PropertyGenerator::setStatic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Code\Generator\PropertyGenerator
的用法示例。
在下文中一共展示了PropertyGenerator::setStatic方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateCode
/**
* Generate the code for the given className and primaryKey and write it in a file.
* @param string $className The class name.
* @param string $primaryKey The primary key.
*/
private function generateCode($className, $primaryKey)
{
$tags = $this->config->getTags();
$class = new ClassGenerator();
$docblock = DocBlockGenerator::fromArray(array('shortDescription' => ucfirst($className) . ' model class', 'longDescription' => 'This is a model class generated with DavidePastore\\ParisModelGenerator.', 'tags' => $tags));
$idColumn = new PropertyGenerator('_id_column');
$idColumn->setStatic(true)->setDefaultValue($primaryKey);
$table = new PropertyGenerator('_table');
$table->setStatic(true)->setDefaultValue($className);
$tableUseShortName = new PropertyGenerator('_table_use_short_name');
$tableUseShortName->setStatic(true)->setDefaultValue(true);
$namespace = $this->config->getNamespace();
$extendedClass = '\\Model';
if (isset($namespace) && !empty($namespace)) {
$class->setNamespaceName($this->config->getNamespace());
}
$class->setName(ucfirst($className))->setDocblock($docblock)->setExtendedClass($extendedClass)->addProperties(array($idColumn, $table, $tableUseShortName));
$file = FileGenerator::fromArray(array('classes' => array($class), 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => ucfirst($className) . ' class file', 'longDescription' => null, 'tags' => $tags))));
$generatedCode = $file->generate();
$directory = $this->config->getDestinationFolder() . $namespace;
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
$filePath = $directory . "/" . $class->getName() . ".php";
if (file_exists($filePath) && !$this->force) {
$helper = $this->getHelper('question');
$realPath = realpath($filePath);
$this->output->writeln("\n");
$question = new ConfirmationQuestion('Do you want to overwrite the file "' . $realPath . '"?', false);
if ($helper->ask($this->input, $this->output, $question)) {
$this->writeInFile($filePath, $generatedCode);
}
} else {
$this->writeInFile($filePath, $generatedCode);
}
}
示例2: getGeneratedFile
/**
* @return \Zend\Code\Generator\FileGenerator|null
*/
public function getGeneratedFile()
{
$classConfig = $this->config->getClassConfig($this->fullClassName);
$factoryName = $this->dic->getFactoryClassName($this->fullClassName);
$classReflection = $this->dic->getClassReflection($this->fullClassName);
if (strpos($classReflection->getDocComment(), '@generator ignore') !== false) {
return null;
}
$file = new Generator\FileGenerator();
$factoryClass = new \rg\injektor\generators\FactoryClass($factoryName);
$instanceMethod = new \rg\injektor\generators\InstanceMethod($this->factoryGenerator);
$arguments = array();
$constructorMethodReflection = null;
if ($this->dic->isSingleton($classReflection)) {
$constructorMethodReflection = $classReflection->getMethod('getInstance');
$arguments = $constructorMethodReflection->getParameters();
} else {
if ($classReflection->hasMethod('__construct')) {
$constructorMethodReflection = $classReflection->getMethod('__construct');
$arguments = $constructorMethodReflection->getParameters();
}
}
$isSingleton = $this->dic->isConfiguredAsSingleton($classConfig, $classReflection);
$isService = $this->dic->isConfiguredAsService($classConfig, $classReflection);
$body = '$i = 0;' . PHP_EOL;
if ($isSingleton || $isService) {
$defaultValue = new Generator\PropertyValueGenerator(array(), Generator\ValueGenerator::TYPE_ARRAY, Generator\ValueGenerator::OUTPUT_SINGLE_LINE);
$property = new Generator\PropertyGenerator('instance', $defaultValue, Generator\PropertyGenerator::FLAG_PRIVATE);
$property->setStatic(true);
$factoryClass->addPropertyFromGenerator($property);
}
if ($isSingleton) {
$body .= '$singletonKey = serialize($parameters) . "#" . getmypid();' . PHP_EOL;
$body .= 'if (isset(self::$instance[$singletonKey])) {' . PHP_EOL;
$body .= ' return self::$instance[$singletonKey];' . PHP_EOL;
$body .= '}' . PHP_EOL . PHP_EOL;
}
if ($isService) {
$body .= 'if (self::$instance) {' . PHP_EOL;
$body .= ' return self::$instance;' . PHP_EOL;
$body .= '}' . PHP_EOL . PHP_EOL;
}
$providerClassName = $this->dic->getProviderClassName($classConfig, new \ReflectionClass($this->fullClassName), null);
if ($providerClassName && $providerClassName->getClassName()) {
$argumentFactory = $this->dic->getFullFactoryClassName($providerClassName->getClassName());
$this->factoryGenerator->processFileForClass($providerClassName->getClassName());
$body .= '$instance = \\' . $argumentFactory . '::getInstance(array())->get();' . PHP_EOL;
$this->usedFactories[] = $argumentFactory;
} else {
// constructor method arguments
if (count($arguments) > 0) {
foreach ($arguments as $argument) {
/** @var \ReflectionParameter $argument */
$argumentName = $argument->name;
$this->constructorArguments[] = $argumentName;
$this->constructorArgumentStringParts[] = '$' . $argumentName;
$this->realConstructorArgumentStringParts[] = '$' . $argumentName;
}
$body .= 'if (!$parameters) {' . PHP_EOL;
foreach ($arguments as $argument) {
/** @var \ReflectionParameter $argument */
$injectionParameter = new \rg\injektor\generators\InjectionParameter($argument, $classConfig, $this->config, $this->dic, InjectionParameter::MODE_NO_ARGUMENTS);
try {
if ($injectionParameter->getClassName()) {
$this->factoryGenerator->processFileForClass($injectionParameter->getClassName());
}
if ($injectionParameter->getFactoryName()) {
$this->usedFactories[] = $injectionParameter->getFactoryName();
}
$body .= ' ' . $injectionParameter->getProcessingBody();
} catch (\Exception $e) {
$body .= ' ' . $injectionParameter->getDefaultProcessingBody();
}
}
$body .= '}' . PHP_EOL;
$body .= 'else if (array_key_exists(0, $parameters)) {' . PHP_EOL;
foreach ($arguments as $argument) {
/** @var \ReflectionParameter $argument */
$injectionParameter = new \rg\injektor\generators\InjectionParameter($argument, $classConfig, $this->config, $this->dic, InjectionParameter::MODE_NUMERIC);
try {
if ($injectionParameter->getClassName()) {
$this->factoryGenerator->processFileForClass($injectionParameter->getClassName());
}
if ($injectionParameter->getFactoryName()) {
$this->usedFactories[] = $injectionParameter->getFactoryName();
}
$body .= ' ' . $injectionParameter->getProcessingBody();
} catch (\Exception $e) {
$body .= ' ' . $injectionParameter->getDefaultProcessingBody();
}
}
$body .= '}' . PHP_EOL;
$body .= 'else {' . PHP_EOL;
foreach ($arguments as $argument) {
/** @var \ReflectionParameter $argument */
$injectionParameter = new \rg\injektor\generators\InjectionParameter($argument, $classConfig, $this->config, $this->dic, InjectionParameter::MODE_STRING);
try {
//.........这里部分代码省略.........