/**
  * Load an *Extension or *ExtensionInterface class. If it does not exist, create a stub file and load it.
  *
  * @param string $className
  * @return void
  */
 public function load($className)
 {
     if (!class_exists($className)) {
         if (!$this->isExtension($className) && !$this->isExtensionInterface($className)) {
             return false;
         }
         $resultFileName = $this->generatorIo->generateResultFileName($className);
         if (!$this->generatorIo->fileExists($resultFileName)) {
             $this->generatorIo->makeResultFileDirectory($className);
             $classNameParts = explode('\\', $className);
             /* Split the type name and namespace for the file's contents. */
             $justTypeName = $classNameParts[count($classNameParts) - 1];
             unset($classNameParts[count($classNameParts) - 1]);
             $namespace = implode('\\', $classNameParts);
             if ($this->isExtension($className)) {
                 $content = "namespace {$namespace};\n\nclass {$justTypeName} implements " . "{$justTypeName}Interface\n{\n\n}";
             } else {
                 if ($this->isExtensionInterface($className)) {
                     $content = "namespace {$namespace};\n\ninterface {$justTypeName} extends " . "\\Magento\\Framework\\Api\\ExtensionAttributesInterface \n{\n\n}";
                 }
             }
             $this->generatorIo->writeResultFile($resultFileName, $content);
         }
         include $resultFileName;
     }
     return false;
 }
示例#2
0
 public function testGenerateClassInterceptorWithNamespace()
 {
     $interceptorClassName = self::CLASS_NAME_WITH_NAMESPACE . '\\Interceptor';
     $result = false;
     $generatorResult = $this->_generator->generateClass($interceptorClassName);
     if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
         $result = true;
     }
     $this->assertTrue($result, 'Failed asserting that \'' . (string) $generatorResult . '\' equals \'success\'.');
     if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
         $content = $this->_clearDocBlock(file_get_contents($this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . '\\Interceptor')));
         $expectedContent = $this->_clearDocBlock(file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceInterceptor.php.sample'));
         $this->assertEquals($expectedContent, $content);
     }
 }
 /**
  * Perform validation surrounding source and result classes and entity type
  *
  * @param string $resultEntityType
  * @param string $sourceClassName
  * @param string $resultClass
  * @return string|bool
  */
 protected function shouldSkipGeneration($resultEntityType, $sourceClassName, $resultClass)
 {
     if (!$resultEntityType || !$sourceClassName) {
         return self::GENERATION_ERROR;
     } else {
         if ($this->definedClasses->isClassLoadableFromDisc($resultClass)) {
             $generatedFileName = $this->_ioObject->generateResultFileName($resultClass);
             /**
              * Must handle two edge cases: a competing process has generated the class and written it to disc already,
              * or the class exists in committed code, despite matching pattern to be generated.
              */
             if ($this->_ioObject->fileExists($generatedFileName) && !$this->definedClasses->isClassLoadableFromMemory($resultClass)) {
                 $this->_ioObject->includeFile($generatedFileName);
             }
             return self::GENERATION_SKIP;
         } else {
             if (!isset($this->_generatedEntities[$resultEntityType])) {
                 throw new \InvalidArgumentException('Unknown generation entity.');
             }
         }
     }
     return false;
 }
示例#4
0
 public function testGetResultFileName()
 {
     $expectedFileName = self::GENERATION_DIRECTORY . '/class/name.php';
     $this->assertEquals($expectedFileName, $this->_object->generateResultFileName(self::CLASS_NAME));
 }