Completed
Pull Request — master (#6)
by George
05:57 queued 03:58
created

CodeServiceProvider::createFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
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;
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\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\CompilerEngine;
13
use Illuminate\View\Engines\EngineResolver;
14
use Illuminate\View\FileViewFinder;
15
16
class CodeServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Register the service provider.
20
     *
21
     * @return void
22
     */
23
    public function register()
24
    {
25
        $this->registerFactory();
26
27
        $this->registerViewFinder();
28
29
        $this->registerEngineResolver();
30
    }
31
32
    /**
33
     * Register the view environment.
34
     *
35
     * @return void
36
     */
37
    public function registerFactory()
38
    {
39
        $this->app->singleton('code', function ($app) {
40
            // Next we need to grab the engine resolver instance that will be used by the
41
            // environment. The resolver will be used by an environment to get each of
42
            // the various engine implementations such as plain PHP or Blade engine.
43
            $resolver = $app['code.engine.resolver'];
44
45
            $finder = $app['code.finder'];
46
47
            $factory = $this->createFactory($resolver, $finder, $app['events']);
0 ignored issues
show
Bug introduced by
The method createFactory() does not exist on Ghaskell\Scaffold\CodeServiceProvider. ( Ignorable by Annotation )

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

47
            /** @scrutinizer ignore-call */ 
48
            $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...
48
49
            // We will also set the container instance on this view environment since the
50
            // view composers may be classes registered in the container, which allows
51
            // for great testable, flexible composers for the application developer.
52
            $factory->setContainer($app);
53
54
            $factory->share('app', $app);
55
56
            return $factory;
57
        });
58
    }
59
60
    /**
61
     * Register the view finder implementation.
62
     *
63
     * @return void
64
     */
65
    public function registerViewFinder()
66
    {
67
        $this->app->bind('code.finder', function ($app) {
68
            return new FileViewFinder($app['files'], $app['config']['view.paths']);
69
        });
70
    }
71
72
    /**
73
     * Register the engine resolver instance.
74
     *
75
     * @return void
76
     */
77
    public function registerEngineResolver()
78
    {
79
        $this->app->singleton('view.engine.resolver', function () {
80
            $resolver = new EngineResolver;
81
82
            // Next, we will register the various view engines with the resolver so that the
83
            // environment will resolve the engines needed for various views based on the
84
            // extension of view file. We call a method for each of the view's engines.
85
                $this->registerVibroEngine($resolver);
86
87
            return $resolver;
88
        });
89
    }
90
91
    /**
92
     * Register the Blade engine implementation.
93
     *
94
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
95
     * @return void
96
     */
97
    public function registerVibroEngine($resolver)
98
    {
99
        // The Compiler engine requires an instance of the CompilerInterface, which in
100
        // this case will be the Blade compiler, so we'll first create the compiler
101
        // instance to pass into the engine so it can compile the views properly.
102
        $this->app->singleton('vibro.compiler', function () {
103
            return new VibroCompiler(
104
                $this->app['files'], $this->app['config']['view.compiled']
105
            );
106
        });
107
108
        $resolver->register('vibro', function () {
109
            return new CompilerEngine($this->app['vibro.compiler']);
110
        });
111
    }
112
}
113
114