示例#1
0
 /**
  * {@inheritDoc}
  */
 public function __invoke($className)
 {
     if (class_exists($className, false) || !$this->classNameInflector->isProxyClassName($className)) {
         return false;
     }
     $file = $this->fileLocator->getProxyFileName($className);
     if (!file_exists($file)) {
         return false;
     }
     return (bool) (require_once $file);
 }
示例#2
0
 /**
  * {@inheritDoc}
  */
 public function __invoke(string $className) : bool
 {
     if (class_exists($className, false) || !$this->classNameInflector->isProxyClassName($className)) {
         return false;
     }
     $file = $this->fileLocator->getProxyFileName($className);
     if (!file_exists($file)) {
         return false;
     }
     /* @noinspection PhpIncludeInspection */
     return (bool) (require_once $file);
 }
 /**
  * Write generated code to disk and return the class code
  *
  * {@inheritDoc}
  */
 public function generate(ClassGenerator $classGenerator)
 {
     $className = trim($classGenerator->getNamespaceName(), '\\') . '\\' . trim($classGenerator->getName(), '\\');
     $generatedCode = $classGenerator->generate();
     $fileName = $this->fileLocator->getProxyFileName($className);
     $tmpFileName = $fileName . '.' . uniqid('', true);
     // renaming files is necessary to avoid race conditions when the same file is written multiple times
     // in a short time period
     file_put_contents($tmpFileName, "<?php\n\n" . $generatedCode);
     rename($tmpFileName, $fileName);
     return $generatedCode;
 }
 /**
  * Write generated code to disk and return the class code
  *
  * {@inheritDoc}
  */
 public function generate(ClassGenerator $classGenerator)
 {
     $className = trim($classGenerator->getNamespaceName(), '\\') . '\\' . trim($classGenerator->getName(), '\\');
     $generatedCode = $classGenerator->generate();
     $fileName = $this->fileLocator->getProxyFileName($className);
     set_error_handler($this->emptyErrorHandler);
     try {
         $this->writeFile("<?php\n\n" . $generatedCode, $fileName);
     } catch (FileNotWritableException $fileNotWritable) {
         restore_error_handler();
         throw $fileNotWritable;
     }
     restore_error_handler();
     return $generatedCode;
 }
示例#5
0
 /**
  * @covers \ProxyManager\Autoloader\Autoloader::__invoke
  */
 public function testWillAutoloadExistingFile()
 {
     $namespace = 'Foo';
     $className = UniqueIdentifierGenerator::getIdentifier('Bar');
     $fqcn = $namespace . '\\' . $className;
     $fileName = sys_get_temp_dir() . '/foo_' . uniqid() . '.php';
     file_put_contents($fileName, '<?php namespace ' . $namespace . '; class ' . $className . '{}');
     $this->classNameInflector->expects($this->once())->method('isProxyClassName')->with($fqcn)->will($this->returnValue(true));
     $this->fileLocator->expects($this->once())->method('getProxyFileName')->will($this->returnValue($fileName));
     $this->assertTrue($this->autoloader->__invoke($fqcn));
     $this->assertTrue(class_exists($fqcn, false));
 }