Exemplo n.º 1
0
 private function scanBundle(Bundle $bundle, BundleService $bundleService)
 {
     $bundleService->addBundle($bundle);
     if ($bundle->hasBundles()) {
         $rootNamespace = $bundle->getNamespace();
         $rootDir = $bundle->getBundlesDir();
         $bundles = Chain::create(scandir($bundle->getBundlesDir()))->filter(function (string $dir) use($rootDir) {
             return $dir != '.' && $dir != '..' && is_dir($rootDir . '/' . $dir);
         })->map(function (string $dir) use($rootNamespace) {
             $bundleClassName = "{$rootNamespace}\\{$dir}\\{$dir}Bundle";
             if (!class_exists($bundleClassName)) {
                 $bundleClassName = "{$rootNamespace}\\Bundles\\{$dir}\\{$dir}Bundle";
                 if (!class_exists($bundleClassName)) {
                     throw new \Exception(sprintf('No Bundle available for bundle `%s`', $bundleClassName));
                 }
             }
             if (!is_subclass_of($bundleClassName, Bundle::class)) {
                 throw new \Exception(sprintf('Bundle `%s` should implements interface %s', $bundleClassName, Bundle::class));
             }
             return new $bundleClassName();
         })->array;
         foreach ($bundles as $bundle) {
             $this->scanBundle($bundle, $bundleService);
         }
     }
 }
Exemplo n.º 2
0
 /**
  * @test
  * @covers Cocur\Chain\Link\Diff::diff()
  */
 public function diffDiffsChainWithChain()
 {
     /** @var \Cocur\Chain\Link\Diff $mock */
     $mock = $this->getMockForTrait('Cocur\\Chain\\Link\\Diff');
     $mock->array = [0, 1, 2];
     $mock->diff(Chain::create([1, 2, 3]));
     $this->assertEquals([0], $mock->array);
 }
Exemplo n.º 3
0
 /**
  * @test
  * @covers Cocur\Chain\Link\Combine::combine()
  */
 public function combineValuesAndKeysWithChainTest()
 {
     /** @var \Cocur\Chain\Link\Combine $mock */
     $mock = $this->getMockForTrait('Cocur\\Chain\\Link\\Combine');
     $keys = Chain::create(['foo', 'bar']);
     $values = Chain::create([42, 43]);
     $this->assertEquals(['foo' => 42, 'bar' => 43], $mock->combine($keys, $values)->array);
 }
Exemplo n.º 4
0
 /**
  * @test
  * @covers Cocur\Chain\Link\Merge::merge()
  */
 public function mergeMergesRecursiveChain()
 {
     /** @var \Cocur\Chain\Link\Merge $mock */
     $mock = $this->getMockForTrait('Cocur\\Chain\\Link\\Merge');
     $mock->array = ['foo' => [0, 1], 'bar' => ['a', 'b']];
     $mock->merge(Chain::create(['foo' => [2, 3], 'bar' => ['c', 'd']]), ['recursive' => true]);
     $this->assertEquals(['foo' => [0, 1, 2, 3], 'bar' => ['a', 'b', 'c', 'd']], $mock->array);
 }
Exemplo n.º 5
0
 /**
  * @test
  * @covers Cocur\Chain\Link\IntersectKey::intersectKey()
  */
 public function intersectKeyIntersectsWithChain()
 {
     /** @var \Cocur\Chain\Link\IntersectKey $mock */
     $mock = $this->getMockForTrait('Cocur\\Chain\\Link\\IntersectKey');
     $mock->array = ['a' => 1, 'b' => 2, 'c' => 3];
     $mock->intersectKey(Chain::create(['a' => 3, 'b' => 4, 'd' => 5]));
     $this->assertEquals(['a' => 1, 'b' => 2], $mock->array);
 }
Exemplo n.º 6
0
 /**
  * @test
  * @covers Cocur\Chain\Link\IntersectAssoc::intersectAssoc()
  */
 public function intersectAssocIntersectsWithChain()
 {
     /** @var \Cocur\Chain\Link\IntersectAssoc $mock */
     $mock = $this->getMockForTrait('Cocur\\Chain\\Link\\IntersectAssoc');
     $mock->array = [1, 2, 3];
     $mock->intersectAssoc(Chain::create([3, 2, 1]));
     $this->assertContains(2, $mock->array);
     $this->assertNotContains(1, $mock->array);
     $this->assertNotContains(3, $mock->array);
 }
Exemplo n.º 7
0
 public function build()
 {
     $request = $this->apiDocsBuildRequest;
     $result = Chain::create($request->getDirectories())->filter(function ($directory) {
         return is_dir($directory);
     })->map(function ($directory) {
         $definitions = Chain::create(scandir($directory))->filter(function ($input) use($directory) {
             return $input !== '.' && $input !== '..' && is_dir("{$directory}/{$input}");
         })->map(function ($prefix) use($directory) {
             $subDirectory = "{$directory}/{$prefix}";
             return ['prefix' => $prefix, 'files' => $this->recursiveListYAMLFiles($subDirectory)];
         })->array;
         if (file_exists($globalFile = "{$directory}/global.yml")) {
             $definitions[] = ['prefix' => null, 'files' => [$globalFile]];
         }
         return $definitions;
     })->map(function ($definitions) {
         $config = [];
         foreach ($definitions as $definition) {
             $prefix = $definition['prefix'];
             $files = $definition['files'];
             $ymlParsedFiles = Chain::create($files)->map(function ($file) {
                 return Yaml::parse(file_get_contents($file));
             })->reduce(function ($carry, $item) {
                 return array_merge_recursive($carry, $item);
             }, []);
             if ($prefix === null) {
                 $config = $config + $ymlParsedFiles;
             } else {
                 if (!isset($config[$prefix])) {
                     $config[$prefix] = [];
                 }
                 $config = array_merge_recursive($config, [$prefix => $ymlParsedFiles]);
             }
         }
         return $config;
     })->reduce(function ($config, $yml) {
         return array_merge_recursive($config, $yml);
     }, ['paths' => [], 'definitions' => [], 'responses' => []]);
     if (!count($result['paths'])) {
         unset($result['paths']);
     }
     if (!count($result['definitions'])) {
         unset($result['definitions']);
     }
     if (!count($result['responses'])) {
         unset($result['responses']);
     }
     return $result;
 }
Exemplo n.º 8
0
 private function mergeBundleConfigs(ConfigService $configService, array $bundles)
 {
     array_walk($bundles, function (Bundle $bundle) use($configService) {
         $dir = $bundle->getConfigDir();
         if (is_dir($dir)) {
             Chain::create(scandir($dir))->filter(function ($input) use($dir) {
                 return is_file($dir . '/' . $input) && preg_match('/\\.config.php$/', $input);
             })->map(function ($input) use($dir) {
                 return require $dir . '/' . $input;
             })->map(function (array $config) use($configService) {
                 $configService->merge($config);
                 return null;
             });
         }
     });
 }
Exemplo n.º 9
0
 private function setupEvents(Application $app, EventEmitter $emitter, $eventConfigFile)
 {
     if (file_exists($eventConfigFile)) {
         $callback = (require $eventConfigFile);
         if (!is_callable($callback)) {
             throw new \Exception(sprintf('Event config `%s` should returns a Callable with EventEmitter and Container argument', $eventConfigFile));
         }
         $result = $callback($emitter, $app->getContainer());
         if (is_array($result) && count($result)) {
             Chain::create($result)->map(function (string $script) use($app) {
                 return $app->getContainer()->get($script);
             })->map(function (EventsBootstrapInterface $script) use($emitter) {
                 $script->up($emitter);
             });
         }
     }
 }
Exemplo n.º 10
0
 public function fetch(Filter $filter) : array
 {
     $result = [];
     $scripts = Chain::create($this->bundlesService->getBundles())->filter(function (Bundle $bundle) {
         return $bundle instanceof FrontlineBundleInjectable;
     })->map(function (FrontlineBundleInjectable $bundle) {
         return $bundle->getFrontlineScripts();
     })->reduce(function (array $carry, array $scripts) {
         return array_merge($carry, $scripts);
     }, []);
     $scripts = array_map(function (string $script) {
         return $this->container->get($script);
     }, $scripts);
     foreach ($filter->filter($scripts) as $script) {
         if ($script instanceof FrontlineScript) {
             $result = array_merge_recursive($result, $script());
         } else {
             throw new \Exception();
         }
     }
     return $result;
 }
Exemplo n.º 11
0
<?php

use Cocur\Chain\Chain;
require_once __DIR__ . '/../vendor/autoload.php';
$arr = array_filter(array_map(function ($v) {
    return rand(0, $v);
}, array_fill(0, 10, 20)), function ($v) {
    return $v & 1;
});
print_r($arr);
$chain = Chain::fill(0, 10, 20)->map(function ($v) {
    return rand(0, $v);
})->filter(function ($v) {
    return $v & 1;
});
print_r($chain->array);
Exemplo n.º 12
0
 /**
  * @test
  * @covers Cocur\Chain\Chain::count()
  */
 public function chainIsCountable()
 {
     $c = Chain::create([0, 1, 2]);
     $this->assertInstanceOf('\\Countable', $c);
     $this->assertEquals(3, count($c));
 }
Exemplo n.º 13
0
 public function removeDirectory($directory)
 {
     $this->directories = Chain::create($this->directories)->filter(function ($input) use($directory) {
         return $input !== $directory;
     });
 }