Passed
Pull Request — master (#7)
by George
02:21
created

CodeServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerFactory() 0 20 1
A registerViewFinder() 0 4 1
A register() 0 7 1
A registerEngineResolver() 0 11 1
A registerVibroEngine() 0 13 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: georg
5
 * Date: 5/28/2018
6
 * Time: 1:09 PM
7
 */
8
9
namespace Ghaskell\Scaffold\Providers;
10
11
use Illuminate\Support\ServiceProvider;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ghaskell\Scaffold\Providers\ServiceProvider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Illuminate\View\Engines\EngineResolver;
13
use Illuminate\View\FileViewFinder;
14
15
class CodeServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Register the service provider.
19
     *
20
     * @return void
21
     */
22
    public function register()
23
    {
24
        $this->registerFactory();
25
26
        $this->registerViewFinder();
27
28
        $this->registerEngineResolver();
29
    }
30
31
    /**
32
     * Register the view environment.
33
     *
34
     * @return void
35
     */
36
    public function registerFactory()
37
    {
38
        $this->app->singleton('code', function ($app) {
39
            // Next we need to grab the engine resolver instance that will be used by the
40
            // environment. The resolver will be used by an environment to get each of
41
            // the various engine implementations such as plain PHP or Blade engine.
42
            $resolver = $app['code.engine.resolver'];
43
44
            $finder = $app['code.finder'];
45
46
            $factory = $this->createFactory($resolver, $finder, $app['events']);
0 ignored issues
show
Bug introduced by
The method createFactory() does not exist on Ghaskell\Scaffold\Providers\CodeServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            /** @scrutinizer ignore-call */ 
47
            $factory = $this->createFactory($resolver, $finder, $app['events']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
48
            // We will also set the container instance on this view environment since the
49
            // view composers may be classes registered in the container, which allows
50
            // for great testable, flexible composers for the application developer.
51
            $factory->setContainer($app);
52
53
            $factory->share('app', $app);
54
55
            return $factory;
56
        });
57
    }
58
59
    /**
60
     * Register the view finder implementation.
61
     *
62
     * @return void
63
     */
64
    public function registerViewFinder()
65
    {
66
        $this->app->bind('code.finder', function ($app) {
67
            return new FileViewFinder($app['files'], $app['config']['view.paths']);
68
        });
69
    }
70
71
    /**
72
     * Register the engine resolver instance.
73
     *
74
     * @return void
75
     */
76
    public function registerEngineResolver()
77
    {
78
        $this->app->singleton('view.engine.resolver', function () {
79
            $resolver = new EngineResolver;
80
81
            // Next, we will register the various view engines with the resolver so that the
82
            // environment will resolve the engines needed for various views based on the
83
            // extension of view file. We call a method for each of the view's engines.
84
                $this->registerVibroEngine($resolver);
85
86
            return $resolver;
87
        });
88
    }
89
90
    /**
91
     * Register the Blade engine implementation.
92
     *
93
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
94
     * @return void
95
     */
96
    public function registerVibroEngine($resolver)
97
    {
98
        // The Compiler engine requires an instance of the CompilerInterface, which in
99
        // this case will be the Blade compiler, so we'll first create the compiler
100
        // instance to pass into the engine so it can compile the views properly.
101
        $this->app->singleton('vibro.compiler', function () {
102
            return new VibroCompiler(
0 ignored issues
show
Bug introduced by
The type Ghaskell\Scaffold\Providers\VibroCompiler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
                $this->app['files'], $this->app['config']['view.compiled']
104
            );
105
        });
106
107
        $resolver->register('vibro', function () {
108
            return new CodeCompilerEngine($this->app['vibro.compiler']);
0 ignored issues
show
Bug introduced by
The type Ghaskell\Scaffold\Providers\CodeCompilerEngine was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
109
        });
110
    }
111
}
112
113