• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/cli/ormannotatecommand.php
  • Класс: BitrixMainCliOrmAnnotateCommand
  • Вызов: OrmAnnotateCommand::execute
protected function execute(InputInterface $input, OutputInterface $output)
{
	$output->writeln([
		'Entity Scanner',
		'==============',
		'',
	]);

	$time = getmicrotime();
	$memoryBefore = memory_get_usage();

	/** @var Exception[] $exceptions deferred errors */
	$exceptions = [];

	// handle already known classes (but we don't know their modules)
	// as long as there are no any Table by default, we can ignore it
	//$this->handleClasses($this->getDeclaredClassesDiff(), $input, $output);

	// skip already defined classes
	$this->getDeclaredClassesDiff();

	// scan dirs
	$inputModules = [];
	$inputModulesRaw = $input->getOption('modules');

	if (!empty($inputModulesRaw) && $inputModulesRaw != 'all')
	{
		$inputModules = explode(',', $inputModulesRaw);
	}

	$dirs = $this->getDirsToScan($inputModules, $input, $output);

	foreach ($dirs as $dir)
	{
		$this->scanDir($dir, $input, $output);
	}

	// scan for bitrix entities
	$this->scanBitrixEntities($inputModules, $input, $output);

	// get classes from outside regular filesystem (e.g. iblock, hlblock)
	try
	{
		$this->handleVirtualClasses($inputModules, $input, $output);
	}
	catch (Exception $e)
	{
		$exceptions[] = $e;
	}

	// output file path
	$filePath = $input->getArgument('output');
	$filePath = ($filePath[0] == '/')
		? $filePath // absolute
		: getcwd().'/'.$filePath; // relative

	// handle entities
	$annotations = [];

	// get current annotations
	if (!$input->getOption('clean') && file_exists($filePath) && is_readable($filePath))
	{
		$rawAnnotations = explode('/* '.static::ANNOTATION_MARKER, file_get_contents($filePath));

		foreach ($rawAnnotations as $rawAnnotation)
		{
			if ($rawAnnotation[0] === ':')
			{
				$endPos = mb_strpos($rawAnnotation, ' */');
				$entityClass = mb_substr($rawAnnotation, 1, $endPos - 1);
				//$annotation = substr($rawAnnotation, $endPos + 3 + strlen(PHP_EOL));

				$annotations[$entityClass] = '/* '.static::ANNOTATION_MARKER.rtrim($rawAnnotation);
			}
		}
	}

	// add/rewrite new entities
	foreach ($this->entitiesFound as $entityMeta)
	{
		try
		{
			$entityClass = $entityMeta['class'];
			$annotateUfOnly = $entityMeta['ufOnly'];

			$entity = Entity::getInstance($entityClass);
			$entityAnnotation = static::annotateEntity($entity, $annotateUfOnly);

			if (!empty($entityAnnotation))
			{
				$annotations[$entityClass] = "/* ".static::ANNOTATION_MARKER.":{$entityClass} */".PHP_EOL;
				$annotations[$entityClass] .= $entityAnnotation;
			}
		}
		catch (Exception $e)
		{
			$exceptions[] = $e;
		}
	}

	// write to file
	$fileContent = 'writeln('Map has been saved to: '.$filePath);

	// summary stats
	$time = round(getmicrotime() - $time, 2);
	$memoryAfter = memory_get_usage();
	$memoryDiff = $memoryAfter - $memoryBefore;

	$output->writeln('Scanned modules: '.join(', ', $this->modulesScanned));
	$output->writeln('Scanned files: '.$this->filesIncluded);
	$output->writeln('Found entities: '.count($this->entitiesFound));
	$output->writeln('Time: '.$time.' sec');
	$output->writeln('Memory usage: '.(round($memoryAfter/1024/1024, 1)).'M (+'.(round($memoryDiff/1024/1024, 1)).'M)');
	$output->writeln('Memory peak usage: '.(round(memory_get_peak_usage()/1024/1024, 1)).'M');

	if (!empty($exceptions))
	{
		$io = new SymfonyStyle($input, $output);

		foreach ($exceptions as $e)
		{
			$io->warning('Exception: '.$e->getMessage().PHP_EOL.$e->getTraceAsString());
		}
	}

	return 0;
}