getInstance() 공개 정적인 메소드

Returns the single instance of kernel
public static getInstance ( ) : static
리턴 static
예제 #1
0
 /**
  * Returns an annotation reader
  *
  * @return Reader $reader
  */
 private static function getReader()
 {
     if (!self::$annotationReader) {
         self::$annotationReader = AspectKernel::getInstance()->getContainer()->get('aspect.annotation.reader');
     }
     return self::$annotationReader;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('Start up application with supplied config...');
     $config = $input->getArgument('applicationConfig');
     $path = stream_resolve_include_path($config);
     if (!is_readable($path)) {
         throw new \InvalidArgumentException("Invalid loader path: {$config}");
     }
     // Init the application once using given config
     // This way the late static binding on the AspectKernel
     // will be on the goaop-zf2-module kernel
     \Zend\Mvc\Application::init(include $path);
     if (!class_exists(AspectKernel::class, false)) {
         $message = "Kernel was not initialized yet. Maybe missing module Go\\ZF2\\GoAopModule in config {$path}";
         throw new \InvalidArgumentException($message);
     }
     $kernel = AspectKernel::getInstance();
     $options = $kernel->getOptions();
     if (empty($options['cacheDir'])) {
         throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured');
     }
     $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
     $iterator = $enumerator->enumerate();
     $totalFiles = iterator_count($iterator);
     $output->writeln("Total <info>{$totalFiles}</info> files to process.");
     $iterator->rewind();
     set_error_handler(function ($errno, $errstr, $errfile, $errline) {
         throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
     });
     $index = 0;
     $errors = [];
     foreach ($iterator as $file) {
         if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
             $output->writeln("Processing file <info>{$file->getRealPath()}</info>");
         }
         $isSuccess = null;
         try {
             // This will trigger creation of cache
             file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ . SourceTransformingLoader::FILTER_IDENTIFIER . '/resource=' . $file->getRealPath());
             $isSuccess = true;
         } catch (\Exception $e) {
             $isSuccess = false;
             $errors[$file->getRealPath()] = $e;
         }
         if ($output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
             $output->write($isSuccess ? '.' : '<error>E</error>');
             if (++$index % 50 == 0) {
                 $output->writeln("({$index}/{$totalFiles})");
             }
         }
     }
     restore_error_handler();
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
         foreach ($errors as $file => $error) {
             $message = "File {$file} is not processed correctly due to exception: {$error->getMessage()}";
             $output->writeln($message);
         }
     }
     $output->writeln('<info>Done</info>');
 }
 /**
  * Returns an annotation reader
  *
  * @return Reader $reader
  */
 private static function getReader()
 {
     if (!self::$annotationReader) {
         // TODO: ugly global dependecy, decide how to inject it more friendly
         self::$annotationReader = AspectKernel::getInstance()->getContainer()->get('aspect.annotation.reader');
     }
     return self::$annotationReader;
 }
예제 #4
0
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln("Loading aspect kernel for warmup...");
     $loader = $input->getArgument('loader');
     $path = stream_resolve_include_path($loader);
     if (!is_readable($path)) {
         throw new \InvalidArgumentException("Invalid loader path: {$loader}");
     }
     include_once $path;
     if (!class_exists('Go\\Core\\AspectKernel', false)) {
         $message = "Kernel was not initialized yet, please configure it in the {$path}";
         throw new \InvalidArgumentException($message);
     }
     $kernel = AspectKernel::getInstance();
     $options = $kernel->getOptions();
     if (empty($options['cacheDir'])) {
         throw new \InvalidArgumentException("Cache warmer require the `cacheDir` options to be configured");
     }
     $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($options['appDir'], \FilesystemIterator::SKIP_DOTS));
     /** @var \CallbackFilterIterator|\SplFileInfo[] $iterator */
     $iterator = new \CallbackFilterIterator($iterator, $this->getFileFilter($options));
     $totalFiles = iterator_count($iterator);
     $output->writeln("Total <info>{$totalFiles}</info> files to process.");
     $iterator->rewind();
     set_error_handler(function ($errno, $errstr, $errfile, $errline) {
         throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
     });
     $index = 0;
     $errors = array();
     foreach ($iterator as $file) {
         if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
             $output->writeln("Processing file <info>{$file->getRealPath()}</info>");
         }
         $isSuccess = null;
         try {
             // This will trigger creation of cache
             file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ . SourceTransformingLoader::FILTER_IDENTIFIER . "/resource=" . $file->getRealPath());
             $isSuccess = true;
         } catch (\Exception $e) {
             $isSuccess = false;
             $errors[$file->getRealPath()] = $e;
         }
         if ($output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
             $output->write($isSuccess ? '.' : '<error>E</error>');
             if (++$index % 50 == 0) {
                 $output->writeln("({$index}/{$totalFiles})");
             }
         }
     }
     restore_error_handler();
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
         foreach ($errors as $file => $error) {
             $message = "File {$file} is not processed correctly due to exception: {$error->getMessage()}";
             $output->writeln($message);
         }
     }
     $output->writeln("<info>Done</info>");
 }
예제 #5
0
 /**
  * Returns a joinpoint for specific function in the namespace
  *
  * @param string $joinPointName Special joinpoint name
  * @param string $namespace Name of the namespace
  *
  * @return FunctionInvocation
  */
 public static function getJoinPoint($joinPointName, $namespace)
 {
     /** @var LazyAdvisorAccessor $accessor */
     static $accessor = null;
     if (!$accessor) {
         $accessor = AspectKernel::getInstance()->getContainer()->get('aspect.advisor.accessor');
     }
     $advices = self::$functionAdvices[$namespace][AspectContainer::FUNCTION_PREFIX][$joinPointName];
     $filledAdvices = array();
     foreach ($advices as $advisorName) {
         $filledAdvices[] = $accessor->{$advisorName};
     }
     return new ReflectionFunctionInvocation($joinPointName, $filledAdvices);
 }
예제 #6
0
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $loader = $input->getArgument('loader');
     $path = stream_resolve_include_path($loader);
     if (!is_readable($path)) {
         throw new \InvalidArgumentException("Invalid loader path: {$loader}");
     }
     include_once $path;
     if (!class_exists(AspectKernel::class, false)) {
         $message = "Kernel was not initialized yet, please configure it in the {$path}";
         throw new \InvalidArgumentException($message);
     }
     $this->aspectKernel = AspectKernel::getInstance();
 }
예제 #7
0
 public static function getJoinPoint($traitName, $className, $joinPointType, $pointName)
 {
     /** @var LazyAdvisorAccessor $accessor */
     static $accessor = null;
     if (!isset($accessor)) {
         $aspectKernel = AspectKernel::getInstance();
         $accessor = $aspectKernel->getContainer()->get('aspect.advisor.accessor');
     }
     $advices = self::$traitAdvices[$traitName][$joinPointType][$pointName];
     $filledAdvices = [];
     foreach ($advices as $advisorName) {
         $filledAdvices[] = $accessor->{$advisorName};
     }
     $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $pointName . '➩', $filledAdvices);
     return $joinpoint;
 }
예제 #8
0
 public static function getJoinPoint($traitName, $className, $joinPointType, $pointName)
 {
     /** @var LazyAdvisorAccessor $accessor */
     static $accessor = null;
     if (!self::$invocationClassMap) {
         $aspectKernel = AspectKernel::getInstance();
         $accessor = $aspectKernel->getContainer()->get('aspect.advisor.accessor');
         self::setMappings($aspectKernel->hasFeature(Features::USE_CLOSURE), $aspectKernel->hasFeature(Features::USE_SPLAT_OPERATOR));
     }
     $advices = self::$traitAdvices[$traitName][$joinPointType][$pointName];
     $filledAdvices = array();
     foreach ($advices as $advisorName) {
         $filledAdvices[] = $accessor->{$advisorName};
     }
     $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $pointName . '➩', $filledAdvices);
     return $joinpoint;
 }
예제 #9
0
 /**
  * Wrap advices with joinpoint object
  *
  * @param array|Advice[] $classAdvices Advices for specific class
  * @param string $className Name of the original class to use
  *
  * @throws \UnexpectedValueException If joinPoint type is unknown
  *
  * NB: Extension should be responsible for wrapping advice with join point.
  *
  * @return array|Joinpoint[] returns list of joinpoint ready to use
  */
 protected static function wrapWithJoinPoints($classAdvices, $className)
 {
     /** @var LazyAdvisorAccessor $accessor */
     static $accessor = null;
     if (!isset($accessor)) {
         $aspectKernel = AspectKernel::getInstance();
         $accessor = $aspectKernel->getContainer()->get('aspect.advisor.accessor');
     }
     $joinPoints = [];
     foreach ($classAdvices as $joinPointType => $typedAdvices) {
         // if not isset then we don't want to create such invocation for class
         if (!isset(self::$invocationClassMap[$joinPointType])) {
             continue;
         }
         foreach ($typedAdvices as $joinPointName => $advices) {
             $filledAdvices = [];
             foreach ($advices as $advisorName) {
                 $filledAdvices[] = $accessor->{$advisorName};
             }
             $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $joinPointName, $filledAdvices);
             $joinPoints["{$joinPointType}:{$joinPointName}"] = $joinpoint;
         }
     }
     return $joinPoints;
 }
예제 #10
0
 /**
  * Returns an advice from aspect method reflection
  *
  * @param Aspect $aspect Instance of aspect
  * @param ReflectionMethod $refMethod Reflection method of aspect
  *
  * @return callable|object
  */
 public static function fromAspectReflection(Aspect $aspect, ReflectionMethod $refMethod)
 {
     static $useClosure;
     if (!isset($useClosure)) {
         $useClosure = AspectKernel::getInstance()->hasFeature(Features::USE_CLOSURE);
     }
     if ($useClosure) {
         return $refMethod->getClosure($aspect);
     } else {
         return function () use($aspect, $refMethod) {
             return $refMethod->invokeArgs($aspect, func_get_args());
         };
     }
 }
예제 #11
0
 /**
  * {@inheritdoc}
  */
 public function __wakeup()
 {
     $this->container = AspectKernel::getInstance()->getContainer();
 }
예제 #12
0
 /**
  * Unserialize an advice
  *
  * @param array $adviceData Information about advice
  *
  * @return Closure
  */
 public static function unserializeAdvice(array $adviceData)
 {
     $aspectName = $adviceData['aspect'];
     $methodName = $adviceData['method'];
     if (!isset(static::$localAdvicesCache["{$aspectName}->{$methodName}"])) {
         $refMethod = new ReflectionMethod($aspectName, $methodName);
         $aspect = AspectKernel::getInstance()->getContainer()->getAspect($aspectName);
         $advice = $refMethod->getClosure($aspect);
         static::$localAdvicesCache["{$aspectName}->{$methodName}"] = $advice;
     }
     return static::$localAdvicesCache["{$aspectName}->{$methodName}"];
 }